**MIAN, SUFI, AND KHOSHKHOU, "PARTISAN BIAS, ECONOMIC EXPECTATIONS, AND HOUSEHOLD SPENDING"
**REVIEW OF ECONOMICS AND STATISTICS
**REPLICATION CODE

**INSTRUCTIONS: 
**1. ASSIGN PATHS, PUTTING ALL INPUT DATA SETS THAT COME WITH THIS REPLICATION KIT
** INTO THE "INPUTDATA" PATH
**2. CREATE FOLDERS FOR THE OUTPUTDATA AND TABLES FIGURES
**3. RUN ENTIRE CODE

**IMPORTANT NOTE: SOME OF THE DATA SETS ARE PROPRIETARY, CONTACT INFO FOR WHERE TO
**INQUIRE ABOUT AVAILABILITY IS BELOW


**List of input data sets
**CA1_PersonalIncomeSummary.dta: County-level Personal Income from BEA
**CA35_PersonalCurrentTransferReceipts.dta: County-level Taxes/Transfers from BEA
**Caddin.dta: miscellaneous variables, mostly from Census
**CSfinal.dta: county-state level match
**Meventtime.dta: month mapping to event times
**michsurvey_05102017.dta: individual-level Michigan survey data
**Mterms.dta: month mapping to presidential terms
**PoliticalParty.csv: Michigan survey political affiliation of individuals
**SA50_PersonalCurrentTaxes.dta: State-level taxes, from bEA
**Sregiondivision.dta: state match to census region and division
**SYPCE_2017.dta: state-year personal consumption expenditures from BEA
**SYPCPCE_2017.dta: state-year personal consumption expenditures from BEA. per capita
**ZCfinal.dta: zip-county mapping
**Zgeographyfinal.dta: Zip level information for geographical matching
**Zpresvote08.dta: zip level presidential vote shares for 2008
**ZYirsclean.dta: zip-year level income from IRS statistics of income

**Input proprietary data sets: see below for descriptions and how to obtain
**Igalluppanel.dta: Individual Gallup level data
**Cprezvotes.dta: county-level presidential vote shares
**ZMpolkautosales_20180209.dta: Zip-month new auto purchases from IHS Market
**ZMargus.dta; ZMargus1417.dta: Zip-month consumer spending data from Argus

clear matrix
clear mata
clear
set maxvar 20000
set more off
set matsize 10000

*Enter paths here!
global inputdata "C:\Users\asufi\Dropbox (Chicago Booth)\shared\MianSufi\partisanfromscratch202012\replication\inputdata"
global outputdata "C:\Users\asufi\Dropbox (Chicago Booth)\shared\MianSufi\partisanfromscratch202012\replication\outputdata" 
global tablesfigures "C:\Users\asufi\Dropbox (Chicago Booth)\shared\MianSufi\partisanfromscratch202012\replication\tablesfigures" 



*********************************************
**** PART I. CREATING THE DATA FOR ANALYSIS
*********************************************

************************************
**PRELIMINARIES: CODE FOR PUTTING PROPRIETARY DATA SETS
**FROM RAW DATA TO FILES USED BELOW
**YOU MUST CONTACT DATA PROVIDERS TO OBTAIN THESE DATA SETS
************************************

**GALLUP DATA: 2017 US Survey Data: please contact Kris Hodgins, Kris_Hodgins@gallup.com,
**202.715.3184, to inquire about access
**US_DAILY_YYYY_DATA.dta are the raw data sets
**
/*
use "$gallup\US_DAILY_2008_DATA", clear
foreach num of numlist 2009/2017{
append using US_DAILY_`num'_DATA
}
**variable list
# delimit ;
keep
MOTHERLODE_ID
INT_DATE
WP1255
YEAR
MONTH
SURVEY
SA
SB
ZIPCODE
HISPANIC
RACE
WP1220
SC7
D4
D4_1
WP1223
INCOME_SUMMARY
P1
P2
P20
PARTY
PARTYR
PARTYRR
D8B
WP119
H17
WEIGHT_GROUP
EMPLOYMENT2010
WP46
WP9081
WP8859
BAI
EHI
HBI
PHI
WEI
THRIVING
THRIVING_P
SUFFERING_P
P128
M30
WP148
M9
M25A
WP30
WP31
M91
M92
M93
M94
M95
M96
M97
M97A
WP16
WP16_PREDICTED
WP18
WP18_PREDICTED
COMB_WEIGHT
PE_WEIGHT
WB_WEIGHT
COMB_STATEWTSFH
PE_STATEWTSFH
WB_STATEWTSFH
COMB_STATEWTSSH
PE_STATEWTSSH
WB_STATEWTSSH
COMB_STATEWT
PE_STATEWT
WB_STATEWT
COMB_MSAWTS
PE_MSAWTS
WB_MSAWTS
;
# delimit cr

rename MOTHERLODE_ID uniqueid
rename INT_DATE date
format date %td
gen year=year(date)
gen month=mofd(date)
format month %tm
drop YEAR MONTH
rename SURVEY survey
rename SA form
rename SB cell
gen zipcode=real(ZIPCODE)
drop ZIPCODE
rename HISPANIC hispanic
replace hispanic=. if hispanic==3 | hispanic==4
rename RACE race
rename WP1220 age
replace age=. if age==0
replace age=. if age==100
gen children=0 if H17==0
replace children=1 if H17>=1 & H17<98
gen gender="Male" if SC7==1
replace gender="Female" if SC7==2
gen educ=D4
replace educ=. if D4==7 | D4==8
replace educ=D4_1 if D4_1>=1 & D4_1<=4
replace educ=4 if D4_1==5
replace educ=5 if D4_1==6
replace educ=6 if D4_1==7 | D4_1==8
replace educ=. if D4_1==9
# delimit ;
label define educlabel 
1 "< HS Diploma"
2 "HS Diploma"
3 "Technical school"
4 "Some college"
5 "College degree"
6 "Post-college";
# delimit cr
label values educ educlabel
rename WP1223 marstatus
replace marstatus=. if marstatus==6 | marstatus==7
gen married=(marstatus==2)
replace married=. if marstatus==.
rename INCOME_SUMMARY income
replace income=. if income==98 | income==99
rename PARTY party
replace party=. if party==9
rename PARTYR partyr
replace partyr=. if partyr==9
rename PARTYRR partyrr
replace partyrr=. if partyrr==9
rename D8B relig
replace relig=. if relig==3 | relig==4
rename WP119 relig_important
replace relig_important=. if relig_im==98 | relig_im==99
rename WEIGHT_GROUP obesity
rename EMPLOYMENT2010 employment
rename WP46 job
rename WP9081 employment2009
rename WP8859 employment2009alt
rename P128 obama
replace obama=. if obama==3 | obama==4
rename M30 cc
replace cc=. if cc==8 | cc==9
rename WP148 fc
replace fc=. if fc==4 | fc==5
rename M9 yourbiz
replace yourbiz=. if yourbiz==7 | yourbiz==8 | yourbiz==9
rename M25A spend
replace spend=. if spend==9999 | spend==9998
rename WP30 sol_satisf
replace sol_satisf=. if sol_satisf==3
replace sol_satisf=. if sol_satisf==4
rename WP31 sol_better
replace sol_better=. if sol_better==4
replace sol_better=. if sol_better==5
rename M91 spendwatch
rename M92 spendable
rename M93 spendcut
rename M94 spendfeelgood
rename M95 spendworry
rename M96 spendenough1
rename M97 spendenough2
rename M97A finsit
foreach var of varlist spendwatch spendable spendcut spendfeelgood spendworry spendenough? finsit{
replace `var'=. if `var'==3 | `var'==4
}
rename WP16 laddernow
rename WP16_P laddernow_p
rename WP18 ladder5yr
rename WP18_P ladder5yr_p
foreach var of varlist ladder*{
replace `var'=. if `var'==98 | `var'==99
}
rename BAI bai
rename EHI ehi
rename HBI hbi
rename PHI phi
rename WEI wei
rename THRIVING thriving
rename THRIVING_P thriving_p
rename SUFFERING_P suffering_p

# delimit ;
keep
age
bai
cc
cell
children
COMB_MSAWTS
COMB_STAT~FH
COMB_STAT~SH
COMB_STATEWT
COMB_WEIGHT
date
educ
ehi
employm~2009
employment
employment~t
fc
finsit
form
gender
hbi
hispanic
income
job
ladder5yr
ladder5yr_p
laddernow
laddernow_p
married
marstatus
month
obama
obesity
party
partyr
partyrr
PE_MSAWTS
PE_STATEW~FH
PE_STATEW~SH
PE_STATEWT
PE_WEIGHT
phi
race
relig
relig_impo~t
sol_better
sol_satisf
spend
spendable
spendcut
spendenough1
spendenough2
spendfeelg~d
spendwatch
spendworry
suffering_p
survey
thriving
thriving_p
uniqueid
WB_MSAWTS
WB_STATEW~FH
WB_STATEW~SH
WB_STATEWT
WB_WEIGHT
wei
year
yourbiz
zipcode;
# delimit cr
sort zipcode
merge m:1 zipcode using "$inputdata\Zgeographyfinal"
keep if _merge==3
drop _merge
drop statematch county countymatch latitude longitude elevation msa
rename county_fip fips
# delimit ;
order
uniqueid
survey
form
cell
zipcode
fips
cbsa_code
state
date
year
month
age
date
educ
race
hispanic
gender
income
married
marstatus
children
employm*2009
employment
employment*t
job
race
relig
relig_important
obesity
party
partyr
partyrr
obama
cc
fc
sol_better
sol_satisf
spend
spendable
spendcut
spendenough1
spendenough2
spendfeelg~d
spendwatch
spendworry
suffering_p
thriving
thriving_p
phi
bai
ehi
hbi
wei
finsit
ladder5yr
ladder5yr_p
laddernow
laddernow_p
yourbiz
COMB_MSAWTS
COMB_STAT~FH
COMB_STAT~SH
COMB_STATEWT
COMB_WEIGHT
PE_MSAWTS
PE_STATEW~FH
PE_STATEW~SH
PE_STATEWT
PE_WEIGHT
WB_MSAWTS
WB_STATEW~FH
WB_STATEW~SH
WB_STATEWT
WB_WEIGHT
;
# delimit cr
sort uniqueid
save "$inputdata\Igalluppanel", replace
*/


***County level data Presential votes
**data came from: http://uselectionatlas.org/BOTTOM/store_data.php
/*
**1992
insheet using "$elections\1992_0_0_2.csv", clear
gen fips=real(v1)
gen votes1992=real(v4)
gen dem1992=real(v5)
gen rep1992=real(v6)
gen perot1992=real(v7)
keep fips *1992
drop if fips==.
sort fips
save "$elections\t1992", replace
**1996
insheet using "$elections\1996_0_0_2.csv", clear
gen fips=real(v1)
gen votes1996=real(v4)
gen dem1996=real(v5)
gen rep1996=real(v6)
gen perot1996=real(v7)
keep fips *1996
drop if fips==.
sort fips
save "$elections\t1996", replace
**2000
insheet using "$elections\2000_0_0_2.csv", clear
gen fips=real(v1)
gen votes2000=real(v4)
gen dem2000=real(v5)
gen rep2000=real(v6)
gen nader2000=real(v7)
gen buch2000=real(v8)
keep fips *2000
drop if fips==.
sort fips
save "$elections\t2000", replace
**2004
insheet using "$elections\2004_0_0_2.csv", clear
gen fips=real(v1)
gen votes2004=real(v4)
gen dem2004=real(v5)
gen rep2004=real(v6)
gen nader2004=real(v7)
keep fips *2004
drop if fips==.
sort fips
save "$elections\t2004", replace
**2008
insheet using "$elections\2008_0_0_2.csv", clear
gen fips=real(v1)
gen votes2008=real(v4)
gen dem2008=real(v5)
gen rep2008=real(v6)
gen nader2008=real(v7)
keep fips *2008
drop if fips==.
sort fips
save "$elections\t2008", replace
**2012
insheet using "$elections\2012_0_0_2.csv", clear
rename fips v1
gen fips=real(v1)
gen votes2012=real(totalvote)
gen dem2012=real(barack)
gen rep2012=real(willard)
keep fips *2012
drop if fips==.
sort fips
save "$elections\t2012", replace
**2016
insheet using "$elections\2016_0_0_2.csv", clear
gen fips=real(v1)
gen votes2016=real(v4)
gen dem2016=real(v5)
gen rep2016=real(v6)
gen johns2016=real(v8)
keep fips *2016
drop if fips==.
sort fips
save "$elections\t2016", replace

use "$elections\t1992", clear
foreach num of numlist 1996 2000 2004 2008 2012 2016{
merge 1:1 fips using $elections\t`num'
drop _merge
erase $elections\t`num'.dta
}
sort fips
label data "Presidential votes by county, 1992 to 2016"
foreach num of numlist 1992 1996 2000 2004 2008 2012 2016{
gen reppct`num'=rep`num'/votes`num'
gen dempct`num'=dem`num'/votes`num'
}
save "$inputdata\Cprezvotes", replace
erase "$elections\t1992.dta"
*/

/*
***Zip-year-month auto sales from IHS Markit (former R.L. Polk)
***Contact: Kristi Rutkowski, kristi.rutkowski@ihsmarkit.com, 248 728 7871
***The raw data sets are in Excel, and were copy and pasted manually into Stata
*2017
use "$autosales\ZYMpolkautosalesraw_2017_20180209.dta", clear
gen january=string(m201701)
gen february=string(m201702)
gen march=string(m201703)
gen april=string(m201704)
gen may=string(m201705)
gen june=string(m201706)
gen july=string(m201707)
gen august=string(m201708)
gen september=string(m201709)
gen october=string(m201710)
*gen november=string(m201711)
*gen december=string(m201712)
rename reg_zip zipcode
drop if zipcode==.
drop m20*
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2017", replace

*2016
use "$autosales\ZYMpolkautosalesraw_2016_20170413.dta", clear
gen january=string(m201601)
gen february=string(m201602)
gen march=string(m201603)
gen april=string(m201604)
gen may=string(m201605)
gen june=string(m201606)
gen july=string(m201607)
gen august=string(m201608)
gen september=string(m201609)
gen october=string(m201610)
gen november=string(m201611)
gen december=string(m201612)
rename reg_zip zipcode
drop if zipcode==.
drop m20*
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2016", replace

*2015
use "$autosales\ZYMpolkautosalesraw_2015_20170403.dta", clear
gen january=string(m201501)
gen february=string(m201502)
gen march=string(m201503)
gen april=string(m201504)
gen may=string(m201505)
gen june=string(m201506)
gen july=string(m201507)
gen august=string(m201508)
gen september=string(m201509)
gen october=string(m201510)
gen november=string(m201511)
gen december=string(m201512)
rename reg_zip zipcode
drop if zipcode==.
drop m20*
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2015", replace


*2014
use "$autosales\ZYMpolkautosalesraw_2014_final.dta", clear
gen january=string(m201401)
gen february=string(m201402)
gen march=string(m201403)
gen april=string(m201404)
gen may=string(m201405)
gen june=string(m201406)
gen july=string(m201407)
gen august=string(m201408)
gen september=string(m201409)
gen october=string(m201410)
gen november=string(m201411)
gen december=string(m201412)
rename reg_zip zipcode
drop if zipcode==.
drop m20*
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2014", replace

*2013
use "$autosales\ZYMpolkautosalesraw_2013_20140224.dta", clear
rename reg_zip zipcode
gen january=string(_01)
gen february=string(_02)
gen march=string(_03)
gen april=string(_04)
gen may=string(_05)
gen june=string(_06)
gen july=string(_07)
gen august=string(_08)
gen september=string(_09)
gen october=string(_10)
gen november=string(_11)
gen december=string(_12)
drop _01 _02 _03 _04 _05 _06 _07 _08 _09 _10 _11 _12
drop if zipcode==.
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2013", replace
*2012
use "$autosales\ZYMpolkautosalesraw_2012_20130304.dta", clear
rename reg_zip zipcode
gen january=string(_01)
gen february=string(_02)
gen march=string(_03)
gen april=string(_04)
gen may=string(_05)
gen june=string(_06)
gen july=string(_07)
gen august=string(_08)
gen september=string(_09)
gen october=string(_10)
gen november=string(_11)
gen december=string(_12)
drop _01 _02 _03 _04 _05 _06 _07 _08 _09 _10 _11 _12
drop if zipcode==.
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2012", replace
*2011
use "$autosales\ZYMpolkautosalesraw_2011janjune", clear
drop if zipcode==.
sort zipcode
merge 1:1 zipcode using "$autosales\ZYMpolkautosalesraw_2011july"
drop _merge
sort zipcode
merge 1:1 zipcode using "$autosales\ZYMpolkautosalesraw_2011august"
drop _merge
sort zipcode
merge 1:1 zipcode using "$autosales\ZYMpolkautosalesraw_2011september"
drop _merge
sort zipcode
merge 1:1 zipcode using "$autosales\ZYMpolkautosalesraw_2011october"
drop _merge
sort zipcode
merge 1:1 zipcode using "$autosales\ZYMpolkautosalesraw_2011november"
drop _merge
sort zipcode
merge 1:1 zipcode using "$autosales\ZYMpolkautosalesraw_2011december"
drop _merge
sort zipcode
save "$autosales\ZYMpolkautosalesraw_2011", replace
clear
gen year=.
foreach num of numlist 1998/2017{
cd "$autosales"
append using ZYMpolkautosalesraw_`num'
replace year=`num' if year==.
}
drop if zipcode==.
foreach var of varlist january-total{
replace `var'=subinstr(`var',",","",1000)
rename `var' t
gen `var'=real(t)
drop t
}
drop total
expand 12
sort zipcode year
by zipcode year: gen month=_n
gen autosales=.
replace autosales=january if month==1
replace autosales=february if month==2
replace autosales=march if month==3
replace autosales=april if month==4
replace autosales=may if month==5
replace autosales=june if month==6
replace autosales=july if month==7
replace autosales=august if month==8
replace autosales=september if month==9
replace autosales=october if month==10
replace autosales=november if month==11
replace autosales=december if month==12
drop if autosales==.
keep zipcode year month autosales
rename month m
gen month_s=string(year)+"m"+string(m)
gen month=monthly(month_s, "YM")
format month %tm
order zipcode month autosales
keep zipcode month autosales
sort zipcode month
label data "New auto sales, by zip code month, from Polk"
save "$input\ZMpolkautosales_20180209", replace
*/



/*
***Zip-year-month household spending from Argus Information and Advisory Services
***Contact: Ravi Urs, rurs@argusinformation.com
***raw data for 2008-2013: HARP_MCC.txt transformed into ZIP_MONTH_TOTAL_SPENDING.dta
use "$argus\ZIP_MONTH_TOTAL_SPENDING.dta", clear
gen zipcode=real(ZipCode)
sum TxnAmount if zipcode==.
sum TxnAmount if zipcode~=.
drop if zipcode==.
gen month=monthly(substr(string(PeriodID),1,4)+"m"+substr(string(PeriodID),5,2),"YM")
format month %tm
rename NumberofTxns argusnum
rename TxnAmount argus
keep zipcode month argus*
collapse (sum) argus*, by(zipcode month)
order zipcode month
sort zipcode month
save "$inputdata\ZMargus", replace
***raw data for 2008-2013: HARP_MCC.txt transformed into ZIP_MONTH_TOTAL_SPENDING.dta
insheet using "$argus\HARP_MCC_Rollup_20180116_Final.txt", clear delimit("|")
rename zipcode ZipCode
rename txnamount TxnAmount
rename numberoftxns NumberofTxns
rename periodid PeriodID
gen zipcode=real(ZipCode)
sum TxnAmount if zipcode==.
sum TxnAmount if zipcode~=.
drop if zipcode==.
gen month=monthly(substr(string(PeriodID),1,4)+"m"+substr(string(PeriodID),5,2),"YM")
format month %tm
rename NumberofTxns argusnum
rename TxnAmount argus
keep zipcode month argus*
collapse (sum) argus*, by(zipcode month)
order zipcode month
sort zipcode month
save "$inputdata\ZMargus1417", replace
*/


************************************
**PUT TOGETHER INDIVIDUAL LEVEL MICHIGAN SURVEY
************************************

*new political affiliation variable
insheet using "$inputdata\PoliticalParty.csv", clear
rename yyyymm YYYYMM
rename id ID
sort YYYYMM ID
save "$outputdata\poladd", replace

*build data
use "$inputdata\michsurvey_05102017", clear
sort YYYYMM ID
merge 1:1 YYYYMM ID using "$outputdata\poladd"
keep if _merge==1 | _merge==3
drop _merge
sort mydate
merge m:1 mydate using "$inputdata\Mterms"
keep if _merge==1 | _merge==3
drop _merge
gen yearpseudo=year
replace yearpseudo=year-1 if month<=5
gen cycle=1996 if yearpseudo>=1995 & yearpseudo<=1998
replace cycle=2000 if yearpseudo>=1999 & yearpseudo<=2002
replace cycle=2004 if yearpseudo>=2003 & yearpseudo<=2006
replace cycle=2008 if yearpseudo>=2007 & yearpseudo<=2010
replace cycle=2012 if yearpseudo>=2011 & yearpseudo<=2014
replace cycle=2016 if yearpseudo>=2015
keep if cycle~=. | polaff~=.
**defining cycles and sample dates
gen election=.
replace election=1996 if mydate>=430 & mydate<=454
replace election=2000 if mydate>=478 & mydate<=502
replace election=2004 if mydate>=526 & mydate<=550
replace election=2008 if mydate>=574 & mydate<=598
replace election=2012 if mydate>=622 & mydate<=646
replace election=2016 if mydate>=670 & mydate<=694
**create same demographics used to predict votes
gen uniqueid=caseid
rename FIPS fips
rename RACE raceold
gen race="White" if raceold==1
replace race="Black" if raceold==2
replace race="Hispanic" if raceold==3
replace race="Other" if raceold==4 | raceold==5
gen gender="Male" if SEX==1
replace gender="Female" if SEX==2
gen children=(NUMKID>=1)
gen age8=1 if AGE>=18 & AGE<=24
replace age8=2 if AGE>=25 & AGE<=29
replace age8=3 if AGE>=30 & AGE<=39
replace age8=4 if AGE>=40 & AGE<=44
replace age8=5 if AGE>=45 & AGE<=49
replace age8=6 if AGE>=50 & AGE<=59
replace age8=7 if AGE>=60 & AGE<=64
replace age8=8 if AGE>=65 & AGE~=.
gen income=1 if INCOME<30000
replace income=2 if INCOME>=30000 & INCOME<50000
replace income=3 if INCOME>=50000 & INCOME<100000
replace income=4 if INCOME>=100000 & INCOME~=.
gen married=(MARRY==1)
replace married=. if MARRY==.
gen educ=1 if EDUC<=2
replace educ=2 if EDUC==3
replace educ=3 if EDUC==4
replace educ=4 if EDUC==5
replace educ=5 if EDUC==6
gen weight=WT
*coding variables that proxy current conditions and expectations of future financial condition
gen ec_ics=ICS
gen ec_icc=ICC
gen ec_ice=ICE
gen ec_gov=(((3-GOVT)/2)*100)
gen ec_durables=(((3-DUR)/2)*100)
gen ec_home=(((3-HOM)/2)*100)
gen ec_car=(((3-CAR)/2)*100)
gen ec_pexp=(((3-PEXP)/2)*100)
gen ec_e5y=(100*((3-BUS5)/2)) 
gen ec_e12m=(100*((3-BUS12)/2))
gen ec_dur=PAGO if DUR<=5
replace ec_dur=-1 if DUR==5
replace ec_dur=0 if DUR==3
gen ec_pago=PAGO if PAGO<=5
replace ec_pago=-1 if PAGO==5
replace ec_pago=0 if PAGO==3
gen ec_pago5=PAGO5 if PAGO5<=5
replace ec_pago5=-1 if PAGO5==5
replace ec_pago5=0 if PAGO5==3
gen ec_bus12=1 if BUS12<=2
replace ec_bus12=0 if BUS12==3
replace ec_bus12=-1 if BUS12==4 | BUS12==5
gen ec_bus5=1 if BUS5<=2
replace ec_bus5=0 if BUS5==3
replace ec_bus5=-1 if BUS5==4 | BUS5==5
gen ec_pexp5=PEXP5 if PEXP5<=5
replace ec_pexp5=-1 if PEXP5==5
replace ec_pexp5=0 if PEXP5==3
gen ec_inexq1=INEXQ1 if INEXQ1<=5
replace ec_inexq1=-1 if INEXQ1==5
replace ec_inexq1=0 if INEXQ1==3
gen ec_rinc=RINC if RINC<=5
replace ec_rinc=-1 if RINC==5
replace ec_rinc=0 if RINC==3
gen ec_bago=BAGO if BAGO<=5
replace ec_bago=-1 if BAGO==5
replace ec_bago=0 if BAGO==3
gen ec_bexp=BEXP if BEXP<=5
replace ec_bexp=-1 if BEXP==5
replace ec_bexp=0 if BEXP==3
gen ec_emp=(UNEMP-3)/2 if UNEMP<=5
gen ec_stock=INVEST==1 
replace ec_stock=. if INVEST==.
gen ec_stockamt=log(INVAMT)
order uniqueid race gender age8 income married educ weight
sort uniqueid
*put in state
sort fips
merge m:1 fips using "$inputdata\CSfinal"
keep if _merge==1 | _merge==3
drop _merge
sort uniqueid
*labels
# delimit ;
label define agelabel 
1 "18 to 24"
2 "25 to 29"
3 "30 to 39"
4 "40 to 44"
5 "45 to 49"
6 "50 to 59"
7 "60 to 64"
8 "over 64";
# delimit cr
label values age8 agelabel
label define childlabel 0 "No children" 1 "Has children"
label values children childlabel 
label define marriedlabel 0 "Not married" 1 "Married"
label values married marriedlabel 
# delimit ;
label define inclabel 
1 "<30K"
2 "30K to 50K"
3 "50K to 100K"
4 ">100K";
# delimit cr
label values income inclabel
# delimit ;
label define educlabel 
1 "< HS Diploma"
2 "HS Diploma"
3 "Some college"
4 "College degree"
5 "Post-college";
# delimit cr
label values educ educlabel
encode race, gen(racenum)
encode state, gen(statenum)
rename gender t
encode t, gen(gender)
drop t
label var ec_ice "Idx consumer expectations"
label var ec_pexp "Future financial condition"
label var ec_e12m "Bus conditions, 12 months"
label var ec_e5y "Bus conditions, 5 years"
label var ec_icc "Idx current conditions"
label var ec_gov "Views on gov ec policy"
label var ec_durables "Good time to buy durables"
label var ec_car "Good time to buy car"
gen partyr=1 if polaff==1
replace partyr=3 if polaff==2
replace partyr=2 if polaff==3
replace partyr=1 if polaff==3 & polcrd==1
replace partyr=3 if polaff==3 & polcrd==5
# delimit ;
label define partyrlabel 
1 "Republican"
2 "Independent"
3 "Democrat"
;
# delimit cr
label values partyr partyrlabel
gen party2=0 if partyr==3
replace party2=1 if partyr==1
# delimit ;
label define party2label 
0 "Democrat"
1 "Republican"
;
# delimit cr
label values party2 party2label
# delimit ;
label define termlabel
0 "Carter"
1 "Reagan1"
2 "Reagan2"
3 "BushHW"
4 "Clinton1"
5 "Clinton2"
6 "BushW1"
7 "BushW2"
8 "Obama1"
9 "Obama2"
10 "Trump"
;
# delimit cr
label values term termlabel
label values termalt termlabel
gen aroundelection=(term~=. & termalt==.)
drop if ec_ice==.
save "$outputdata\IMmichiganfinalFULL", replace
keep if party2~=.
*normalizing these variable to have mean 0 and std. dev of 1
foreach var of varlist ec_ics ec_icc ec_ice ec_gov ec_durables ec_home ec_car ec_e5y ec_e12m ec_pexp{
sum `var'
replace `var'=(`var'-r(mean))/r(sd)
}
save "$outputdata\IMmichiganfinal", replace



************************************
**PUT TOGETHER INDIVIDUAL LEVEL GALLUP SURVEY
************************************
use "$inputdata\Igalluppanel", clear /*proprietary data, must obtain, see above*/
gen yearpseudo=year
rename month mydate
sort mydate
merge m:1 mydate using "$inputdata\Mterms"
keep if _merge==1 | _merge==3
drop _merge
gen month=month(date)
replace yearpseudo=year-1 if month<=5
gen cycle=1996 if yearpseudo>=1995 & yearpseudo<=1998
replace cycle=2000 if yearpseudo>=1999 & yearpseudo<=2002
replace cycle=2004 if yearpseudo>=2003 & yearpseudo<=2006
replace cycle=2008 if yearpseudo>=2007 & yearpseudo<=2010
replace cycle=2012 if yearpseudo>=2011 & yearpseudo<=2014
replace cycle=2016 if yearpseudo>=2015
keep if cycle~=.
**defining cycles and sample dates
gen election=.
replace election=1996 if mydate>=430 & mydate<=454
replace election=2000 if mydate>=478 & mydate<=502
replace election=2004 if mydate>=526 & mydate<=550
replace election=2008 if mydate>=574 & mydate<=598
replace election=2012 if mydate>=622 & mydate<=646
replace election=2016 if mydate>=670 & mydate<=694
**create same demographics used to predict votes
rename race raceold
decode raceold, gen(race)
replace race="Other" if race=="Asian"
rename age AGE
gen age8=1 if AGE>=18 & AGE<=24
replace age8=2 if AGE>=25 & AGE<=29
replace age8=3 if AGE>=30 & AGE<=39
replace age8=4 if AGE>=40 & AGE<=44
replace age8=5 if AGE>=45 & AGE<=49
replace age8=6 if AGE>=50 & AGE<=59
replace age8=7 if AGE>=60 & AGE<=64
replace age8=8 if AGE>=65 & AGE~=.
rename income INCOME
gen income=1 if INCOME<4
replace income=2 if INCOME>=5 & INCOME<7
replace income=3 if INCOME>=7 & INCOME<9
replace income=4 if INCOME>=9 & INCOME~=.
rename educ EDUC
gen educ=1 if EDUC<=1
replace educ=2 if EDUC==2
replace educ=3 if EDUC==3 | EDUC==4
replace educ=4 if EDUC==5
replace educ=5 if EDUC==6
gen weight=COMB_WEIGHT

*coding variables that proxy current conditions and expectations of future financial condition
gen ec_fc=4-fc
gen ec_cc=cc
gen ec_sol=3-sol_better
gen ec_finsit=3-finsit
gen ec_laddernow=laddernow
gen ec_ladder5yr=ladder5yr
gen ec_yourbiz=4-yourbiz
gen ec_spendable=2-spendable
gen ec_spendcut=spendcut
gen ec_spendenough1=2-spendenough1
gen ec_spendenough2=2-spendenough2
gen ec_spendfeelgood=2-spendfeelgood
gen ec_spendworry=spendworry
gen ec_spendwatch=spendwatch
order uniqueid race gender age8 income married educ weight
*labels
# delimit ;
label define agelabel 
1 "18 to 24"
2 "25 to 29"
3 "30 to 39"
4 "40 to 44"
5 "45 to 49"
6 "50 to 59"
7 "60 to 64"
8 "over 64";
# delimit cr
label values age8 agelabel
label define childlabel 0 "No children" 1 "Has children"
label values children childlabel 
label define marriedlabel 0 "Not married" 1 "Married"
label values married marriedlabel 
# delimit ;
label define inclabel 
1 "<30K"
2 "30K to 50K"
3 "50K to 100K"
4 ">100K";
# delimit cr
label values income inclabel
label values educ educlabel
encode race, gen(racenum)
encode state, gen(statenum)
rename gender t
encode t, gen(gender)
drop t
sort uniqueid
gen party2=0 if partyr==3
replace party2=1 if partyr==1
# delimit ;
label define party2label 
0 "Democrat"
1 "Republican"
;
# delimit cr
label values party2 party2label
# delimit ;
label define termlabel
0 "Carter"
1 "Reagan1"
2 "Reagan2"
3 "BushHW"
4 "Clinton1"
5 "Clinton2"
6 "BushW1"
7 "BushW2"
8 "Obama1"
9 "Obama2"
10 "Trump"
;
# delimit cr
label values term termlabel
label values termalt termlabel
gen aroundelection=(term~=. & termalt==.)
*final drops, can change later
count
tab partyr
drop if partyr==.
count
*normalizing these variable to have mean 0 and std. dev of 1
foreach var of varlist ec_fc ec_cc ec_sol ec_finsit ec_laddernow ec_ladder5yr ec_yourbiz ec_spend*{
sum `var'
replace `var'=(`var'-r(mean))/r(sd)
}
sort uniqueid
save "$outputdata\IMgallupfinal", replace



**************************************
*MEASURES OF PARTISANSHIP, GEOGRAPHY
**************************************

*gallup county level
use "$outputdata\IMgallupfinal", clear /*proprietary data, must obtain, see above*/
gen yearalt=year
replace yearalt=year+1 if month>=11
drop if partyr==2
gen Crep_gall=(party2==1)
gen Cdem_gall=(party2==0)
gen Cgallnum=1
collapse (mean) Crep_gall Cdem_gall (rawsum) Cgallnum, by(fips yearalt)
rename yearalt year
reshape wide C*, i(fips) j(year)
sort fips
save "$outputdata\Crep_gall", replace
*gallup zip code level
use "$outputdata\IMgallupfinal", clear
gen yearalt=year
replace yearalt=year+1 if month>=11
drop if partyr==.
drop if partyr==2
gen Zrep_gall=(party2==1)
gen Zdem_gall=(party2==0)
gen Zgallnum=1
collapse (mean) Zrep_gall Zdem_gall (rawsum) Zgallnum, by(zipcode yearalt)
rename yearalt year
reshape wide Z*, i(zipcode) j(year)
sort zipcode
save "$outputdata\Zrep_gall", replace
*Michigan county level
use "$outputdata\IMmichiganfinal", clear
gen yearalt=year
replace yearalt=year+1 if month>=11
drop if partyr==.
drop if partyr==2
drop if fips==.
gen Crep_mich=(party2==1)
gen Cdem_mich=(party2==0)
gen Cmichnum=1
collapse (mean) Crep_mich Cdem_mich (rawsum) Cmichnum, by(fips yearalt)
rename yearalt year
reshape wide C*, i(fips) j(year)
sort fips
save "$outputdata\Crep_mich", replace
*final county level
use "$outputdata\Crep_gall", clear
merge 1:1 fips using "$outputdata\Crep_mich"
drop _merge
sort fips
merge 1:1 fips using "$inputdata\Cprezvotes" /*proprietary data, must obtain, see above*/
drop _merge
foreach num of numlist 2000 2004 2008 2012 2016{
drop reppct`num'
gen reppct`num'=rep`num'/(rep`num'+dem`num')
}
foreach var of varlist rep* dem* votes* perot* nader*{
rename `var' C`var'
}
drop Cdempct*
sort fips
save "$outputdata\Crepfinal", replace
*final zip level
use "$inputdata\Zpresvote08", clear 
rename totpop pop
rename g08p_tv votes2008
gen Zrep2008=(g08p_rp*votes2008)/100
gen Zdem2008=(g08p_dp*votes2008)/100
gen Zreppct2008=Zrep2008/(Zrep2008+Zdem2008)
gen Zdempct2008=Zdem2008/(Zrep2008+Zdem2008)
keep zipcode Z* pop
sort zipcode
merge 1:1 zipcode using "$outputdata\Zrep_gall"
drop _merge
sort zipcode
save "$outputdata\Zrepfinal", replace




************************************
**PUT TOGETHER COUNTY-MONTH AUTOSALES, ARGUS
************************************

**Michigan measures
use "$outputdata\IMmichiganfinal", clear
keep if party2~=.
drop if fips==.
gen one=1
collapse (rawsum) one (mean) ec* party2, by(fips mydate)
rename one michnum
rename party2 party2mich
sort fips mydate
tempfile t1
save `t1', replace
**Gallup measures
use "$outputdata\IMgallupfinal", clear
drop if party2==.
gen one=1
gen Mspendnum=1 if spend~=.
collapse (rawsum) one Mspendnum spend (mean) ec* party2, by(fips mydate)
rename one Mgallnum
rename party2 party2gall
sort fips mydate
tempfile t2
save `t2', replace
**Argus
use "$inputdata\ZMargus", clear  /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
sort zipcode month
by zipcode: keep if _N>=95
rename county_fip fips
collapse (rawsum) argus argusnum, by(fips month)
sort fips month
save "$outputdata\CMargus", replace
**Argus
use "$inputdata\ZMargus1417", clear /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
sort zipcode month
by zipcode: keep if _N>=45
rename county_fip fips
collapse (rawsum) argus argusnum, by(fips month)
rename argus argus1417
rename argusnum argusnum1417
sort fips month
save "$outputdata\CMargus1417", replace
**Auto sales
***some zips missing in some years, so must harmonize data
use "$inputdata\ZMpolkautosales_20180209.dta", clear  /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
drop if year(dofm(month))==2011
sort zipcode month
by zipcode: gen total=_N
keep if total==226
by zipcode: keep if _n==1
sort zipcode
tempfile keepauto
save `keepauto', replace
use "$inputdata\ZMpolkautosales_20180209.dta", clear /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
merge m:1 zipcode using `keepauto'
keep if _merge==3
drop _merge
rename county_fip fips
collapse (sum) autosales, by(fips month)
sort fips month
merge 1:1 fips month using "$outputdata\CMargus"
erase  "$outputdata\CMargus.dta"
drop _merge
sort fips month
merge 1:1 fips month using "$outputdata\CMargus1417"
erase  "$outputdata\CMargus1417.dta"
drop _merge
merge m:1 fips using "$inputdata\Caddin"
keep if _merge==3
drop _merge
merge m:1 fips using "$outputdata\Crepfinal"
keep if _merge==1 | _merge==3
drop _merge
gen mydate=month
merge 1:1 fips mydate using `t1'
keep if _merge==1 | _merge==3
drop _merge
merge 1:1 fips mydate using `t2'
keep if _merge==1 | _merge==3
drop _merge
drop month
gen year=year(dofm(mydate))
gen month=month(dofm(mydate))
gen yearpseudo=year
replace yearpseudo=year-1 if month<=5
gen cycle=1996 if yearpseudo>=1995 & yearpseudo<=1998
replace cycle=2000 if yearpseudo>=1999 & yearpseudo<=2002
replace cycle=2004 if yearpseudo>=2003 & yearpseudo<=2006
replace cycle=2008 if yearpseudo>=2007 & yearpseudo<=2010
replace cycle=2012 if yearpseudo>=2011 & yearpseudo<=2014
replace cycle=2016 if yearpseudo>=2015
keep if cycle~=.
**defining cycles and sample dates
gen election=.
replace election=1996 if mydate>=430 & mydate<=454
replace election=2000 if mydate>=478 & mydate<=502
replace election=2004 if mydate>=526 & mydate<=550
replace election=2008 if mydate>=574 & mydate<=598
replace election=2012 if mydate>=622 & mydate<=646
replace election=2016 if mydate>=670 & mydate<=694
**define republican vote measure
gen repgall=.
gen Ygallnum=.
foreach num of numlist 2008/2017{
replace repgall=Crep_gall`num' if yearpseudo==`num'
replace Ygallnum=Cgallnum`num' if yearpseudo==`num'

}
replace repgall=Crep_gall2008 if yearpseudo==2007
replace Ygallnum=Cgallnum2008 if yearpseudo==2007
gen repvote=.
foreach num of numlist 2000 2004 2008 2012 2016{
replace repvote=Creppct`num' if cycle==`num'
}
replace argus=argus1417 if yearpseudo>=2014
replace argusnum=argusnum1417 if yearpseudo>=2014
replace argus=. if yearpseudo==2013
replace argusnum=. if yearpseudo==2013
keep fips state county czone mydate month year yearpseudo election cycle ec* spend party2gall party2mich michnum Mspendnum Mgallnum Ygallnum autosales argus argusnum repgall repvote pop* cl*
order fips state county czone mydate month year yearpseudo election cycle ec* spend party2gall party2mich michnum Mspendnum Mgallnum Ygallnum autosales argus argusnum repgall repvote pop* cl*
sort fips month
save "$outputdata\CMsentiment", replace


**************************************************
**PUT TOGETHER ZIP-MONTH AUTOSALES, ARGUS 
**************************************************

**get what you need from zip-level prez file
use "$outputdata\Zrepfinal", clear
sort zipcode
merge 1:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
rename county_fip fips
drop county countymatch
sort fips
merge m:1 fips using "$inputdata\CSfinal"
keep if _merge==3
drop _merge
order zipcode fips state county
sort zipcode
save "$outputdata\Zaddin", replace
**Gallup measures
use "$outputdata\IMgallupfinal", clear
drop if party2==.
gen one=1
collapse (rawsum) one (mean) spend ec* party2, by(zipcode mydate)
rename one Mgallnum
rename party2 party2gall
sort zipcode mydate
tempfile t3
save `t3', replace
**Argus
use "$inputdata\ZMargus", clear /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
sort zipcode month
by zipcode: keep if _N>=95
drop county county_fip countymatch
sort zipcode month
tempfile ZMargustemp
save `ZMargustemp', replace
use "$inputdata\ZMargus1417", clear  /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
sort zipcode month
by zipcode: keep if _N>=45
drop county county_fip countymatch
rename argus argus1417
rename argusnum argusnum1417
sort zipcode month
tempfile ZMargustemp1417
save `ZMargustemp1417', replace
**Auto sales
***some zips missing in some years, so must harmonize data
use "$inputdata\ZMpolkautosales_20180209.dta", clear   /*proprietary data, must obtain, see above*/
merge m:1 zipcode using "$inputdata\ZCfinal"
keep if _merge==3
drop _merge
drop if year(dofm(month))==2011
sort zipcode month
by zipcode: gen total=_N
keep if total==226
by zipcode: keep if _n==1
sort zipcode
tempfile keepauto
save `keepauto', replace
use "$inputdata\ZMpolkautosales_20180209.dta", clear
merge m:1 zipcode using `keepauto'
keep if _merge==3
drop _merge
sort zipcode month
merge 1:1 zipcode month using `ZMargustemp'
drop _merge
sort zipcode month
merge 1:1 zipcode month using `ZMargustemp1417'
drop _merge
merge m:1 zipcode using "$outputdata\Zaddin"
keep if _merge==3
drop _merge
gen mydate=month
drop month
gen year=year(dofm(mydate))
gen month=month(dofm(mydate))
sort zipcode mydate
merge 1:1 zipcode mydate using `t3'
keep if _merge==1 | _merge==3
drop _merge
gen yearpseudo=year
replace yearpseudo=year-1 if month<=5
gen cycle=1996 if yearpseudo>=1995 & yearpseudo<=1998
replace cycle=2000 if yearpseudo>=1999 & yearpseudo<=2002
replace cycle=2004 if yearpseudo>=2003 & yearpseudo<=2006
replace cycle=2008 if yearpseudo>=2007 & yearpseudo<=2010
replace cycle=2012 if yearpseudo>=2011 & yearpseudo<=2014
replace cycle=2016 if yearpseudo>=2015
keep if cycle~=.
**defining cycles and sample dates
gen election=.
replace election=1996 if mydate>=430 & mydate<=454
replace election=2000 if mydate>=478 & mydate<=502
replace election=2004 if mydate>=526 & mydate<=550
replace election=2008 if mydate>=574 & mydate<=598
replace election=2012 if mydate>=622 & mydate<=646
replace election=2016 if mydate>=670 & mydate<=694
**define republican vote measure
gen repgall=.
gen Ygallnum=.
foreach num of numlist 2008/2017{
replace repgall=Zrep_gall`num' if yearpseudo==`num'
replace Ygallnum=Zgallnum`num' if yearpseudo==`num'
}
replace repgall=Zrep_gall2008 if yearpseudo==2007
replace Ygallnum=Zgallnum2008 if yearpseudo==2007
gen repvote=.
foreach num of numlist 2008{
replace repvote=Zreppct`num' if cycle==`num'
}
keep if cycle>=2008
replace argus=argus1417 if yearpseudo>=2014
replace argusnum=argusnum1417 if yearpseudo>=2014
replace argus=. if yearpseudo==2013
replace argusnum=. if yearpseudo==2013
keep zipcode fips state county mydate month year yearpseudo election cycle ec* spend party2gall Mgallnum Ygallnum autosales argus argusnum repgall repvote pop*
order zipcode fips state county mydate month year yearpseudo election cycle ec* spend party2gall Mgallnum Ygallnum autosales argus argusnum repgall repvote pop*
sort zipcode month
save "$outputdata\ZMsentiment", replace



************************************
**PUT TOGETHER COUNTY-YEAR BEA DATA
************************************
use "$inputdata\Caddin", clear
merge 1:1 fips using "$inputdata\Cprezvotes"   /*proprietary data, must obtain, see above*/
drop _merge
keep fips county state czone rep* dem* vote* total* pop20?? cl*
sort fips
tempfile t4
save `t4', replace
use "$inputdata\CA1_PersonalIncomeSummary.dta", clear
gen fips=real(geofips)
rename personalIncome pi
rename perCapitaIncome pipc
keep fips year pi* population
sort fips year
tempfile t5
save `t5', replace
use "$inputdata\CA35_PersonalCurrentTransferReceipts.dta", clear
gen fips=real(geofips)
rename cTReGov xtot
rename retDisabBnf xsoc
rename medicalBnf xmed
rename incMaintBnf xinc
rename unemInsCmp xemp
rename vetBnf xvet
rename edTrainAst xedu
rename othTReGov xoth
keep fips year x*
sort fips year
merge 1:1 fips year using `t5'
drop _merge
foreach var of varlist x???{
gen `var'pc=`var'/population
}
drop population
merge m:1 fips using `t4'
gen cycle=2000 if year<=2002
replace cycle=2004 if year>=2003 & year<=2006
replace cycle=2008 if year>=2007 & year<=2010
replace cycle=2012 if year>=2011 & year<=2014
replace cycle=2016 if year>=2015
**define final republican vote measure
foreach num of numlist 2000 2004 2008 2012 2016{
drop reppct`num'
gen reppct`num'=rep`num'/(rep`num'+dem`num')
xtile repq`num'=reppct`num' [aweight=pop`num'], nq(4)
}
gen rep=.
gen repq=.
foreach num of numlist 2000 2004 2008 2012 2016{
replace rep=reppct`num' if cycle==`num'
replace repq=repq`num' if cycle==`num'
}
sort fips year
order fips year
save "$outputdata\CYsentiment", replace




******************************************
**PUT TOGETHER STATE-YEAR CONSUMPTION DATA
******************************************
use "$inputdata\Caddin", clear
merge 1:1 fips using "$inputdata\Cprezvotes"   /*proprietary data, must obtain, see above*/
drop _merge
keep fips county state czone rep* dem* vote* total* pop20??
collapse (sum) rep* dem* vote* total* pop*, by(state)
drop *pct*
sort state
tempfile t6
save `t6', replace
use "$inputdata\SA50_PersonalCurrentTaxes.dta", clear
drop state
gen state=upper(geoname)
gen taxrate= fedIncTaxNet/ persInc
keep state year taxrate
sort state year
tempfile t7
save `t7', replace
use "$inputdata\SYPCPCE_2017.dta", clear
foreach var of varlist pce-nprofit_receive{
rename `var' `var'PC
}
keep state year *PC
tempfile t8
save `t8', replace
use "$inputdata\SYPCE_2017.dta", clear
drop region
merge 1:1 state year using `t8'
drop _merge
replace state=upper(state)
sort state year
merge 1:1 state year using `t7'
drop _merge
merge m:1 state using `t6'
keep if _merge==3
drop _merge
sort state
merge m:1 state using "$inputdata\Sregiondivision"
keep if _merge==1 | _merge==3
drop _merge
gen cycle=2000 if year<=2002
replace cycle=2004 if year>=2003 & year<=2006
replace cycle=2008 if year>=2007 & year<=2010
replace cycle=2012 if year>=2011 & year<=2014
replace cycle=2016 if year>=2015
**define final republican vote measure
foreach num of numlist 2000 2004 2008 2012 2016{
gen reppct`num'=rep`num'/(rep`num'+dem`num')
xtile repq`num'=reppct`num' [aweight=pop`num'], nq(4)
}
gen rep=.
gen repq=.
foreach num of numlist 2000 2004 2008 2012 2016{
replace rep=reppct`num' if cycle==`num'
replace repq=repq`num' if cycle==`num'
}
keep state year region division cycle pce* gd* svc* nprofit* rep reppct* repq* pop* pop* taxrate
sort state year
save "$outputdata\SYsentiment", replace




*********************************************
**PART II: INTERIM DATA SETS
*********************************************


*GALLUP LEVELS AROUND ELECTIONS
cd "$outputdata"
capture program drop _all
program define coef
use "$outputdata\IMgallupfinal", clear
keep if election==`1'
foreach var of varlist ec_fc-ec_spendcut spend{
gen `var'_r=`var' if party2==1
gen `var'_d=`var' if party2==0
}
collapse (mean) *_r *_d, by(mydate)
gen election=`1'
sort mydate
save gallup`1', replace
end
coef 2008
coef 2012
coef 2016
use gallup2008, clear
foreach num of numlist 2012 2016{
append using gallup`num'
erase gallup`num'.dta
}
foreach var of varlist *_r{
label var `var' "Republican"
}
foreach var of varlist *_d{
label var `var' "Democrat"
}
sort mydate
save "$outputdata\gallupgraph", replace

**MICHIGAN LEVELS AROUND ELECTIONS
cd "$outputdata"
capture program drop _all
program define coef
use "$outputdata\IMmichiganfinal", clear
replace election=1984 if mydate>=288 & mydate<=304
keep if election==`1'
foreach var of varlist ec_ice{
gen `var'_r=`var' if party2==1
gen `var'_d=`var' if party2==0
}
collapse (mean) *_r *_d, by(mydate)
gen election=`1'
sort mydate
save gallup`1', replace
end
coef 1984
coef 2008
coef 2016
use gallup1984, clear
foreach num of numlist 2008 2016{
append using gallup`num'
erase gallup`num'.dta
}
foreach var of varlist *_r{
label var `var' "Republican"
}
foreach var of varlist *_d{
label var `var' "Democrat"
}
sort mydate
save "$outputdata\michigangraph", replace

**GALLUP COEFFICIENTS AROUND ELECTIONS
foreach num of numlist 2008/2016{
cd "$outputdata"
use "$outputdata\IMgallupfinal", clear
keep if yearpseudo==`num'
gen lspend=ln(spend)
drop if party2==.
sort mydate
char month[omit] 10 /*October*/
foreach var of varlist ec_fc ec_cc ec_sol ec_laddernow ec_ladder5yr ec_yourbiz lspend{
xi: reg `var' i.month*party2, robust
gen cf`var'_`num'=.
gen hi`var'_`num'=.
gen lo`var'_`num'=.
replace cf`var'_`num'=0 if month==10
replace hi`var'_`num'=0 if month==10
replace lo`var'_`num'=0 if month==10
foreach y of numlist 1/9 11/12{
capture replace cf`var'_`num'=_coef[_ImonXpart_`y'] if month==`y'
capture replace hi`var'_`num'=_coef[_ImonXpart_`y']+2*_se[_ImonXpart_`y'] if month==`y'
capture replace lo`var'_`num'=_coef[_ImonXpart_`y']-2*_se[_ImonXpart_`y'] if month==`y'
}
label var cf`var'_`num' `num'
}
egen monthtag=tag(month)
keep if monthtag==1
keep month mydate cf* hi* lo*
sort mydate
gen eventtime=0 if month==11
gen t1=_n if eventtime==0
egen t2=mean(t1)
replace eventtime=_n-t2
drop t1 t2
sort eventtime
save cf`num', replace
}
use cf2008, clear
foreach num of numlist 2009/2016{
merge 1:1 eventtime using cf`num'
drop _merge
erase cf`num'.dta
}
# delimit ;
label define eventtimelabel 
-5 "Jun"
-4 "Jul"
-3 "Aug"
-2 "Sep"
-1 "Oct"
0 "Nov"
1 "Dec"
2 "Jan"
3 "Feb"
4 "Mar"
5 "Apr"
6 "May";
# delimit cr
label values eventtime eventtimelabel
save "$outputdata\cfgallupgraph", replace

**GALLUP COEFFICIENTS AROUND ELECTIONS, FOR SOME SPEND VARIABLES NOT AVAILABLE UNTIL 2009
foreach num of numlist 2009/2016{
cd "$outputdata"
use "$outputdata\IMgallupfinal", clear
keep if yearpseudo==`num'
gen lspend=ln(spend)
drop if party2==.
sort mydate
char month[omit] 10 /*October*/
foreach var of varlist ec_spendcut ec_spendable{
xi: reg `var' i.month*party2, robust
gen cf`var'_`num'=.
gen hi`var'_`num'=.
gen lo`var'_`num'=.
replace cf`var'_`num'=0 if month==10
replace hi`var'_`num'=0 if month==10
replace lo`var'_`num'=0 if month==10
foreach y of numlist 1/9 11/12{
capture replace cf`var'_`num'=_coef[_ImonXpart_`y'] if month==`y'
capture replace hi`var'_`num'=_coef[_ImonXpart`y']+2*_se[_ImonXpart_`y'] if month==`y'
capture replace lo`var'_`num'=_coef[_ImonXpart_`y']-2*_se[_ImonXpart_`y'] if month==`y'
}
label var cf`var'_`num' `num'
}
egen monthtag=tag(month)
keep if monthtag==1
keep month mydate cf* hi* lo*
sort mydate
gen eventtime=0 if month==11
gen t1=_n if eventtime==0
egen t2=mean(t1)
replace eventtime=_n-t2
drop t1 t2
sort eventtime
save cf`num', replace
}
use cf2009, clear
foreach num of numlist 2010/2016{
merge 1:1 eventtime using cf`num'
drop _merge
erase cf`num'.dta
}
# delimit ;
label define eventtimelabel 
-5 "Jun"
-4 "Jul"
-3 "Aug"
-2 "Sep"
-1 "Oct"
0 "Nov"
1 "Dec"
2 "Jan"
3 "Feb"
4 "Mar"
5 "Apr"
6 "May";
# delimit cr
label values eventtime eventtimelabel
save "$outputdata\cfgallupspendgraph", replace

*MICHIGAN COEFFICIENTS AROUND ELECTIONS USING PARTY2
*2008
cd "$outputdata"
use "$outputdata\IMmichiganfinal", clear
keep if yearpseudo==2008
sort mydate
char month[omit] 10 /*October*/
foreach var of varlist ec_ice ec_pexp ec_e5y ec_e12m ec_gov ec_icc ec_durables ec_home ec_car ec_inexq1{
xi: reg `var' i.month*party2, robust
gen cf`var'_2008=.
gen hi`var'_2008=.
gen lo`var'_2008=.
replace cf`var'_2008=0 if month==10
replace hi`var'_2008=0 if month==10
replace lo`var'_2008=0 if month==10
foreach y of numlist 1/6 9 11/12{
capture replace cf`var'_2008=_coef[_ImonXpart_`y'] if month==`y'
capture replace hi`var'_2008=_coef[_ImonXpart_`y']+2*_se[_ImonXpart_`y'] if month==`y'
capture replace lo`var'_2008=_coef[_ImonXpart_`y']-2*_se[_ImonXpart_`y'] if month==`y'
}
label var cf`var'_2008 2008
}
egen monthtag=tag(month)
keep if monthtag==1
keep month mydate cf* hi* lo*
sort month
merge 1:1 month using "$inputdata\Meventtime"
keep if _merge==3
drop _merge
sort eventtime
order eventtime
save cf2008, replace
*2016
cd "$outputdata"
use "$outputdata\IMmichiganfinal", clear
keep if yearpseudo==2016
sort mydate
char month[omit] 10 /*October*/
foreach var of varlist ec_ice ec_pexp ec_e5y ec_e12m ec_gov ec_icc ec_durables ec_home ec_car ec_inexq1{
xi: reg `var' i.month*party2, robust
gen cf`var'_2016=.
gen hi`var'_2016=.
gen lo`var'_2016=.
replace cf`var'_2016=0 if month==10
replace hi`var'_2016=0 if month==10
replace lo`var'_2016=0 if month==10
foreach y of numlist 2 3 6 7 8 9{
capture replace cf`var'_2016=_coef[_ImonXparty_`y'] if month==`y'
capture replace hi`var'_2016=_coef[_ImonXparty_`y']+2*_se[_ImonXparty_`y'] if month==`y'
capture replace lo`var'_2016=_coef[_ImonXparty_`y']-2*_se[_ImonXparty_`y'] if month==`y'
}
label var cf`var'_2016 2016
}
egen monthtag=tag(month)
keep if monthtag==1
keep month mydate cf* hi* lo*
sort month
merge 1:1 month using "$inputdata\Meventtime"
keep if _merge==3
drop _merge
sort eventtime
order eventtime
save cf2016, replace
use cf2008, clear
foreach num of numlist 2016{
merge 1:1 eventtime using cf`num'
drop _merge
erase cf`num'.dta
}
sort eventtime
save "$outputdata\cfmichigangraph", replace


*AUTOS SALES LEVEL FIGURES, BASED ON COUNTY-LEVEL VOTE SHARES
cd "$outputdata"
capture program drop _all
program define coef
use "$outputdata\CMsentiment", clear
keep if election==`1'
gen t1=autosales if mydate==`2'
egen t2=min(t1), by(fips)
gen autosalesidx=100*(autosales/t2)
drop t1 t2
format mydate %tm
gen autosalesidx_r_p=.
gen autosalesidx_d_p=.
foreach num of numlist `3'/`4'{
reg autosalesidx repvote [aweight=pop`1'] if mydate==`num'
margins, at(repvote=0) post
replace autosalesidx_d_p=_b[_cons] if mydate==`num'
reg autosalesidx repvote [aweight=pop`1'] if mydate==`num'
margins, at(repvote=1) post
replace autosalesidx_r_p=_b[_cons] if mydate==`num'
}
keep mydate election *_d_p *_r_p
sort mydate
by mydate: keep if _n==1
save predauto`1', replace
end
coef 2000 489 480 502
coef 2004 537 526 550
coef 2008 585 574 598
coef 2012 633 622 646
coef 2016 681 670 693
use predauto2000, clear
foreach num of numlist 2004 2008 2012 2016{
append using predauto`num'
erase predauto`num'.dta
}
label var autosalesidx_d_p "Democratic county"
label var autosalesidx_r_p "Republican county"
save "$outputdata\autovotegraph", replace

*ARGUS LEVEL FIGURES, BASED ON COUNTY-LEVEL VOTE SHARES
cd "$outputdata"
capture program drop _all
program define coef
use "$outputdata\CMsentiment", clear
keep if election==`1'
gen t1=argus if mydate==`2'
egen t2=min(t1), by(fips)
gen argusidx=100*(argus/t2)
drop t1 t2
format mydate %tm
gen argusidx_r_p=.
gen argusidx_d_p=.
foreach num of numlist `3'/`4'{
reg argusidx repvote [aweight=pop`1'] if mydate==`num'
margins, at(repvote=0) post
replace argusidx_d_p=_b[_cons] if mydate==`num'
reg argusidx repvote [aweight=pop`1'] if mydate==`num'
margins, at(repvote=1) post
replace argusidx_r_p=_b[_cons] if mydate==`num'
}
keep mydate election *_d_p *_r_p
sort mydate
by mydate: keep if _n==1
save predargus`1', replace
end
coef 2008 585 574 598
coef 2012 633 622 640
coef 2016 681 670 692
use predargus2008, clear
foreach num of numlist 2012 2016{
append using predargus`num'
erase predargus`num'.dta
}
label var argusidx_d_p "Democratic county"
label var argusidx_r_p "Republican county"
save "$outputdata\argusvotegraph", replace


**COEFFICIENT FIGURES FOR AUTO SALES, ARGUS USING VOTE MEASURE
**create coefficient data set for graphs
foreach num of numlist 2000/2016{
cd "$outputdata"
use "$outputdata\CMsentiment", clear
keep if yearpseudo==`num'
sort mydate
char month[omit] 10 /*September*/
gen lautosales=ln(autosales)
gen largus=ln(argus)
gen largusnum=ln(argusnum)
foreach var of varlist lautosales largus largusnum{
capture xi: reg `var' i.month*repvote [aweight=pop`num'], robust
capture gen cf`var'_`num'=.
capture gen hi`var'_`num'=.
capture gen lo`var'_`num'=.
replace cf`var'_`num'=0 if month==10
replace hi`var'_`num'=0 if month==10
replace lo`var'_`num'=0 if month==10
foreach y of numlist 1/9 11/12{
capture replace cf`var'_`num'=_coef[_ImonXrepv_`y'] if month==`y'
capture replace hi`var'_`num'=_coef[_ImonXrepv_`y']+2*_se[_ImonXrepv_`y'] if month==`y'
capture replace lo`var'_`num'=_coef[_ImonXrepv_`y']-2*_se[_ImonXrepv_`y'] if month==`y'
}
label var cf`var'_`num' `num'
}
egen monthtag=tag(month)
keep if monthtag==1
keep month mydate cf* hi* lo*
sort mydate
gen eventtime=0 if month==11
gen t1=_n if eventtime==0
egen t2=mean(t1)
replace eventtime=_n-t2
drop t1 t2
sort eventtime
save cf`num', replace
}
use cf2000, clear
foreach num of numlist 2001/2016{
merge 1:1 eventtime using cf`num'
drop _merge
erase cf`num'.dta
}
# delimit ;
label define eventtimelabel 
-5 "Jun"
-4 "Jul"
-3 "Aug"
-2 "Sep"
-1 "Oct"
0 "Nov"
1 "Dec"
2 "Jan"
3 "Feb"
4 "Mar"
5 "Apr"
6 "May";
# delimit cr
label values eventtime eventtimelabel
save "$outputdata\cfautovotegraph", replace








*********************************************
**** PART III. RESULTS
*********************************************

*************************************************************************
*SUMMARY STATISTICS (TABLE A1) 
*table_sumstats.tex
*************************************************************************

eststo clear
use "$outputdata\IMgallupfinal", clear
label var ec_fc "National economy getting better"
label var spend "Spending yesterday, USD"
label var ec_spendcut "Cutting back spending, inverse"
label var party2 "Republican partisan affiliation (2-party)"
# delimit ;
estpost tabstat
ec_fc spend ec_spendcut party2, stat(count mean sd p10 p90) c(s);
esttab using "$tablesfigures\table_sumstats.tex", replace
cells("count(fmt(%9.0fc)) mean(fmt(2)) sd(fmt(2)) p10(fmt(2)) p90(fmt(2))")
f booktabs noobs nonumber label 
substitute(\midrule "\midrule \multicolumn{5}{l}{\textbf{Individual level - Gallup}}\\"
count "Obs" mean "Mean" sd "SD" p10 "P10" p90 "P90" )
;
# delimit cr

use "$outputdata\IMmichiganfinal", clear
label var ec_ice "Index of consumer expectations"
label var ec_car "Good time to buy car"
label var ec_durables "Good time to buy major items"
label var party2 "Republican partisan affiliation (2-party)"
# delimit ;
estpost tabstat
ec_ice ec_durables ec_car party2, stat(count mean sd p10 p90) c(s);
esttab using "$tablesfigures\table_sumstats.tex", append
cells("count(fmt(%9.0fc)) mean(fmt(2)) sd(fmt(2)) p10(fmt(2)) p90(fmt(2))")
f booktabs noobs nonumber label collabels(none)
substitute(\midrule "\multicolumn{5}{l}{\textbf{Individual level - Michigan}}\\" &\multicolumn{5}{c}{} "" -0.00 "0.00")
;
#delimit cr

use "$outputdata\CMsentiment", clear
keep if year>=2000
replace argus=argus/1000000
label var autosales "Auto purchases"
label var argus "Credit card spending (millions USD)"
label var repvote "Republican vote share, nearest election"
# delimit ;
estpost tabstat
autosales argus repvote [aweight=pop2008], stat(count mean sd p10 p90) c(s);
esttab using "$tablesfigures\table_sumstats.tex", append
cells("count(fmt(%9.0fc)) mean(fmt(2)) sd(fmt(2)) p10(fmt(2)) p90(fmt(2))")
f booktabs noobs nonumber label collabels(none)
substitute(\midrule "\multicolumn{5}{l}{\textbf{County-month level (weighted by population)}}\\" &\multicolumn{5}{c}{} "")
;
#delimit cr



********************************
*FIGURE: LONG RUN PARTISAN BIAS (FIGURE 1)
*fig_long
********************************
**create dataset of all terms
use "$inputdata\Mterms", clear
sort term
by term: keep if _n==1
expand 3
sort term
by term: gen party2=_n-1
keep term party2
sort term
tempfile termtemp
save `termtemp', replace

**michigan
use "$outputdata\IMmichiganfinal", clear
drop if aroundelection==1
replace term=1 if term==2
collapse (mean) ec_ice, by(term party2)
gen diff=abs(ec_ice-ec_ice[_n-1]) if party2==1
keep if diff~=.
replace party2=2
keep term party2 diff
rename diff ec_ice
tempfile icetemp
save `icetemp', replace

use "$outputdata\IMmichiganfinal", clear
drop if party2==.
replace term=1 if term==2
collapse (mean) ec_ice, by(term party2)
append using `icetemp'
sort term party2
merge 1:1 term party2 using `termtemp'
# delimit ;
label define termlabelalt
0 "Carter"
1 "Reagan"
3 "BushHW"
4 "Clinton1"
5 "Clinton2"
6 "BushW1"
7 "BushW2"
8 "Obama1"
9 "Obama2"
10 "Trump"
;
# delimit cr
# delimit ;
label define party2labelalt 
0 "Dem"
1 "Rep"
2 "Diff"
;
# delimit cr
label values term termlabelalt
label values party2 party2labelalt
drop if term==2
gen y0=ec_ice if party2==0
gen y1=ec_ice if party2==1
gen y2=ec_ice if party2==2
drop if ec_ice==.
# delimit ;
graph bar y0 y1 y2, 
over(party2, label(nolabels)) 
over(term, gap(*10))
blabel(group, orient(vertical) size(medsmall))
bar(1, color(gs12) lw(thick)) 
bar(2, color(gs8) lw(thick)) 
bar(3, color(gs1) lw(thick)) 
ylabel(-1 -0.5 0 0.5 1 1.5, labs(large)) legend(off)
ytitle("Average", size(vlarge)) yscale(titlegap(*-4))
title("Michigan: Index of consumer expectations", size(vlarge)) saving("$tablesfigures\fig_longa.gph", replace);
# delimit cr

**gallup
use "$outputdata\IMgallupfinal", clear
drop if party2==.
drop if aroundelection==1
replace termalt=1 if termalt==2
collapse (mean) ec_fc, by(termalt party2)
gen diff=abs(ec_fc-ec_fc[_n-1]) if party2==1
keep if diff~=.
replace party2=2
keep termalt party2 diff
rename diff ec_fc
tempfile ec_fctemp
save `ec_fctemp', replace
use "$outputdata\IMgallupfinal", clear
drop if party2==.
replace termalt=1 if termalt==2
collapse (mean) ec_fc, by(termalt party2)
append using `ec_fctemp'
# delimit ;
label define party2labelalt 
0 "Dem"
1 "Rep"
2 "Diff"
;
# delimit cr
label values party2 party2labelalt
gen y0=ec_fc if party2==0
gen y1=ec_fc if party2==1
gen y2=ec_fc if party2==2
# delimit ;
graph bar y0 y1 y2, 
over(party2, label(nolabels)) 
over(termalt, gap(*10))
blabel(group, orient(vertical) size(medsmall))
bar(1, color(gs12) lw(thick)) 
bar(2, color(gs8) lw(thick)) 
bar(3, color(gs1) lw(thick)) 
ylabel(-1 -0.5 0 0.5 1 1.5, labs(large)) legend(off) 
ytitle("Average", size(vlarge)) yscale(titlegap(*-4))
title("Gallup: Economy getting better", size(vlarge)) saving("$tablesfigures\fig_longb.gph", replace);
# delimit cr
graph combine "$tablesfigures\fig_longb.gph"  "$tablesfigures\fig_longa.gph", xsize(9) ysize(4)
graph export "$tablesfigures\fig_long.eps", replace


**************************************
*fig_R2: Power of Party Affiliationin 
*in Explaining Economy Expectations
**************************************

********************************
*FIGURE: R2 IN LONG RUN (FIGURE A2)
*fig_R2
********************************
**gallup
use "$outputdata\IMgallupfinal", clear
drop if party2==.
drop if aroundelection==1
gen r2=.
foreach num of numlist 7/10{
regress ec_fc party2 if termalt==`num'
replace r2=e(r2) if termalt==`num'
}
sort term
by term: keep if _n==1
keep term r2
# delimit ;
graph bar r2, 
over(term)
ylabel(0 0.1 0.2 0.3 0.4 0.5, labs(large)) legend(off)
ytitle("R-squared from party affiliation", size(vlarge))
title() saving("$tablesfigures\fig_r2.gph", replace);
# delimit cr
graph export "$tablesfigures\fig_r2.eps", replace



*******************************************************************
*TABLE: LONG RUN PARTISAN BIAS REGRESSION (TABLE 1)
*tab_long
*******************************************************************
***gallup
use "$outputdata\IMgallupfinal", clear
keep if party2~=.
drop if aroundelection==1
foreach num of numlist 7/10{
gen party2_`num'=party2*(term==`num')
}
egen cmfe=group(fips month)
egen zmfe=group(zipcode month)

***col 1: baseline
est clear
xi: regress ec_fc party2_7 party2_8 party2_9 party2_10 i.term, robust cluster(fips)
test party2_8==-party2_7
estadd scalar pv87 = r(p), replace
test party2_9==party2_8
estadd scalar pv98 = r(p), replace
test party2_10==-party2_9
estadd scalar pv109 = r(p), replace
estadd local fe "None", replace
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c1

***col 2: countyxmonth fe
xi: areg ec_fc party2_7 party2_8 party2_9 party2_10 i.term, absorb(cmfe) robust cluster(fips)
test party2_8==-party2_7
estadd scalar pv87 = r(p), replace
test party2_9==party2_8
estadd scalar pv98 = r(p), replace
test party2_10==-party2_9
estadd scalar pv109 = r(p), replace
estadd local fe "County\(\times\)month", replace
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c2

***col 3: zipxmonth fe
xi: areg ec_fc party2_7 party2_8 party2_9 party2_10 i.term, absorb(zmfe) robust cluster(fips)
test party2_8==-party2_7
estadd scalar pv87 = r(p), replace
test party2_9==party2_8
estadd scalar pv98 = r(p), replace
test party2_10==-party2_9
estadd scalar pv109 = r(p), replace
estadd local fe "ZIP\(\times\)month", replace
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c3


***psacalc (col1 and col3)
tab term, gen(term_)
xi: regress ec_fc party2_7 party2_8 party2_9 party2_10 term_*, robust //B_o and R_o (0.1963)
local R_o = e(r2)
xi: areg ec_fc party2_7 party2_8 party2_9 party2_10 term_*, absorb(zmfe) robust //B_tilde and R_tilde (0.3809)
local R_tilde = e(r2)
local R_max = 1.5*`R_tilde'
psacalc delta party2_10, mcontrol(party2_7 party2_8 party2_9 term_*) rmax(`R_max')
*psacalc delta party2_10, mcontrol(party2_7 party2_8 party2_9 term_*) rmax(1)
global delta = round(r(delta),0.01)



**michigan
use "$outputdata\IMmichiganfinal", clear
keep if party2~=.
drop if aroundelection==1
replace term=1 if term==2
foreach num of numlist 0 1 7/10{
gen party2_`num'=party2*(term==`num')
}
egen cfe=group(fips month)
***col 4: baseline
xi: regress ec_ice party2_0 party2_1 party2_7 party2_8 party2_9 party2_10 i.term, robust 
*test party2_0==-party2_1
*test party2_7==party2_1
test party2_8==-party2_7
estadd scalar pv87 = r(p), replace
test party2_9==party2_8
estadd scalar pv98 = r(p), replace
test party2_10==-party2_9
estadd scalar pv109 = r(p), replace
estadd local fe "None", replace
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c4

***col 5: countyxmonth fe
xi: areg ec_ice party2_0 party2_1 party2_7 party2_8 party2_9 party2_10 i.term if termalt~=., absorb(cfe) robust cluster(fips)
*test party2_0==-party2_1
*test party2_7==party2_1
test party2_8==-party2_7
estadd scalar pv87 = r(p), replace
test party2_9==party2_8
estadd scalar pv98 = r(p), replace
test party2_10==-party2_9
estadd scalar pv109 = r(p), replace
estadd local fe "County\(\times\)month", replace
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c5

#delimit ;
esttab c1 c2 c3 c4 c5 using "$tablesfigures\table_long.tex", 
width(\linewidth) alignment(c)
varlabels(party2_0 "$\times$ Carter" party2_1 "$\times$ Reagan" party2_7 "$\times$ Bush W 2" party2_8 "$\times$ Obama 1" party2_9 "$\times$ Obama 2" party2_10 "$\times$ Trump" )
keep(party2_0 party2_1 party2_7 party2_8 party2_9 party2_10)
order (party2_0 party2_1 party2_7 party2_8 party2_9 party2_10)
label replace booktabs b(3) se(3) nomtitle noomitted 
refcat(party2_0 "Republican affiliation", nolabel)
mgroups("Gallup: Economy getting better" "Michigan: Index of consumer expectations", 
pattern(1 0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
stats( N r2 plabel pv87 pv98 pv109 fe, fmt(0 3 3 3 3 3 3)
labels("Observations""\(R^{2}\)""P-values of F-tests" "\hspace{0.02cm} Obama 1 + Bush W 2 = 0"
"\hspace{0.02cm} Obama 2 $-$ Obama 1 = 0" "\hspace{0.02cm} Trump + Obama 1 = 0""FE"))
nonotes addnotes("Oster (2019) test statistic for coefficient on Republican $\times$ Trump comparing columns 1 and 3 (null \$\beta^*=0\$, \$R_{max} = 1.5 R\$): \$\delta\$ = $delta." 
"* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01. Heteroskedasticity-robust standard errors clustered at the county level are in parantheses.")
substitute(\_ _);
#delimit cr 




********************************
*FIGURE: AROUND ELECTION, LEVELS AROUND 2016 (FIGURE 2)
*fig_0816
********************************
use "$outputdata\gallupgraph", clear
# delimit ;
graph twoway scatter ec_fc_d ec_fc_r mydate if election==2016,
connect(l l l) msymbol(Oh X Th) msize(large large large) mc(navy maroon dkgreen)
lw(thick thick thick) lc(navy maroon dkgreen) lp(solid dash longdash)
legend(size(small)) xtitle("") ytitle("Average", size(medium))
ylabel(-1 -0.5 0 0.5 1 1.5, labs(medium))
title("Gallup: Economy getting better, 2016", size(medlarge)) xlabel(672 678 684 690 696, labs(small)) xline(682, lc(black) lp(shortdash) lw(thin))
saving("$tablesfigures\fig_trumpa", replace);
# delimit cr
use "$outputdata\michigangraph", clear
# delimit ;
graph twoway scatter ec_ice_d ec_ice_r mydate if election==2016,
connect(l l l) msymbol(Oh X Th) msize(large large large) mc(navy maroon dkgreen)
lw(thick thick thick) lc(navy maroon dkgreen) lp(solid dash longdash)
legend(size(small)) xtitle("") ytitle("Average", size(medium))
ylabel(-1 -0.5 0 0.5 1 1.5, labs(medium))
xlabel(672 678 684 690 696, labs(small)) xline(682, lc(black) lp(shortdash) lw(thin))
title("Michigan: Index of expectations, 2016", size(medlarge))
saving("$tablesfigures\fig_trumpb", replace)
;# delimit cr
use "$outputdata\gallupgraph", clear
# delimit ;
graph twoway scatter ec_fc_d ec_fc_r mydate if election==2008,
connect(l l l) msymbol(Oh X Th) msize(large large large) mc(navy maroon dkgreen)
lw(thick thick thick) lc(navy maroon dkgreen) lp(solid dash longdash)
legend(size(small)) xtitle("") ytitle("Average", size(medium))
ylabel(-1 -0.5 0 0.5 1 1.5, labs(medium))
title("Gallup: Economy getting better, 2008", size(medium)) xlabel(, labs(small)) xline(586, lc(black) lp(shortdash) lw(thin))
saving("$tablesfigures\fig_levelgall08", replace);
# delimit cr
use "$outputdata\michigangraph", clear
# delimit ;
graph twoway scatter ec_ice_d ec_ice_r mydate if election==2008,
connect(l l l) msymbol(Oh X Th) msize(large large large) mc(navy maroon dkgreen)
lw(thick thick thick) lc(navy maroon dkgreen) lp(solid dash longdash)
legend(size(small)) xtitle("") ytitle("Average", size(medium))
ylabel(-1 -0.5 0 0.5 1 1.5, labs(medium))
xlabel(, labs(small)) xline(586, lc(black) lp(shortdash) lw(thin))
title("Michigan: Index of expectations, 2008", size(medlarge))
saving("$tablesfigures\fig_levelmich08", replace)
;
# delimit cr
graph combine "$tablesfigures\fig_trumpa.gph" "$tablesfigures\fig_trumpb.gph" "$tablesfigures\fig_levelgall08" "$tablesfigures\fig_levelmich08", col(2) xsize(10) ysize(8)
graph export "$tablesfigures\fig_0816.eps", replace

********************************
*FIGURE: AROUND ELECTION, LEVELS AROUND OTHER ELECTIONS (FIGURE A3)
*fig_trumpappendix
********************************

use "$outputdata\gallupgraph", clear
# delimit ;
graph twoway scatter ec_fc_r ec_fc_d mydate if election==2012,
connect(l l l) msymbol(Oh X Th) msize(large large large) mc(navy maroon dkgreen)
lw(thick thick thick) lc(navy maroon dkgreen) lp(solid dash longdash)
legend(size(med)) xtitle("") ytitle("Average", size(med))
ylabel(-1 -0.5 0 0.5 1 1.5, labs(medlarge))
title("Gallup: Economy getting better (12)", size(medlarge)) 
xlabel(, labs(medsmall)) xline(634, lc(black) lp(shortdash) lw(thin))
saving("$tablesfigures\fig_levelgall12", replace);
# delimit cr

use "$outputdata\michigangraph", clear
# delimit ;
graph twoway scatter ec_ice_r ec_ice_d mydate if election==1984,
connect(l l l) msymbol(Oh X Th) msize(large large large) mc(navy maroon dkgreen)
lw(thick thick thick) lc(navy maroon dkgreen) lp(solid dash longdash)
legend(size(med)) xtitle("") ytitle("Average", size(med))
ylabel(-1 -0.5 0 0.5 1 1.5, labs(medlarge))
xlabel(, labs(medsmall)) xline(298, lc(black) lp(shortdash) lw(thin))
title("Michigan: Index of consumer expectations (84)", size(medlarge))
saving("$tablesfigures\fig_levelmich84", replace)
;
# delimit cr
# delimit ;
graph combine
"$tablesfigures\fig_levelgall12.gph" "$tablesfigures\fig_levelmich84.gph"  
, xsize(10) ysize(5);
# delimit cr
graph export "$tablesfigures\fig_0816appendix.eps", replace


********************************
*FIGURE: COEFFICIENTS FOR ECONOMIC EXPECTATIONS AROUND ELECTION (FIGURE 3)
*fig_shock
********************************

**graphing coefficients
use "$outputdata\cfgallupgraph", clear
# delimit ;
graph twoway scatter 
cfec_fc_2008 cfec_fc_2009 cfec_fc_2010 cfec_fc_2011
cfec_fc_2012 cfec_fc_2013 cfec_fc_2014 cfec_fc_2015
cfec_fc_2016 eventtime,
connect(l l l l l l l l l) mc(dkgreen gray gray gray lavender gray gray gray maroon)
m(Th i i i Sh i i i Dh) msize(large vtiny vtiny vtiny large vtiny vtiny vtiny large)
lc(dkgreen gray gray gray lavender gray gray gray maroon)
lw(thick thin thin thin thick thin thin thin thick)
lp(longdash solid solid solid shortdash solid solid solid solid)
legend(order(1 5 9) rows(2) size(large)) xtitle("") ytitle("Coefficient on Republican", size(large))
ylabel(-3 -2 -1 0 1 2 3, labs(large))
xlabel(-4 -2 0 2 4 6, labs(large) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Gallup: Economy getting better", size(vlarge)) saving("$tablesfigures\fig_fc.gph", replace);
# delimit cr

use "$outputdata\cfmichigangraph", clear
sort eventtime
foreach var of varlist cfec_ice_2008-loec_inexq1_2016{
	replace `var'=. if `var'==0 & month~=10
}
# delimit ;
graph twoway scatter 
cfec_ice_2008 
cfec_ice_2016 eventtime,
connect(l l) mc(dkgreen maroon)
m(Th Dh) msize(large large)
lc(dkgreen  maroon)
lw(thick thick)
lp(longdash solid)
legend(order(1 2) rows(1) size(large)) xtitle("") ytitle("Coefficient on Republican", size(large))
ylabel(-3 -2 -1 0 1 2 3, labs(large))
xlabel(-4 -2 0 2 4 6, labs(large) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Michigan: Index of consumer expectations", size(vlarge)) saving("$tablesfigures\fig_ice.gph", replace);
# delimit cr
graph combine "$tablesfigures\fig_fc.gph"  "$tablesfigures\fig_ice.gph", xsize(9) ysize(4)
graph export "$tablesfigures\fig_shock.eps", replace



********************************
*TABLE: COEFFICIENTS FOR ECONOMIC EXPECTATIONS AROUND ELECTION (TABLE 2)
*table_shock
********************************
eststo clear 
*Gallup data
use "$outputdata\IMgallupfinal", clear
keep if yearpseudo>=2008 & yearpseudo<=2016
keep if party2~=.
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2008/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)
egen zmfe=group(zipcode month)

*col 1: gallup
# delimit ;
eststo: xi: regress ec_fc
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
estadd local fe "None"
est sto est1

*col 2: gallup, with countyxmonth fe
# delimit ;
eststo: xi: areg ec_fc
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(cmfe) robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
estadd local fe "County\(\times\)month", replace
est sto est2

*col 3: gallup, with zipxmonth fe
# delimit ;
eststo: xi: areg ec_fc
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(zmfe) robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
estadd local fe "ZIP\(\times\)month", replace
est sto est3


***psacalc (col1 and col3)
areg ec_fc party2 _Imon* _Iyea* _Ipost2008_1 _Ipost2012_1 _Ipost2016_1 _IposXparty_1 _IposXpartya1 _IposXpartyb1, absorb(zmfe) robust
local R_tilde = e(r2)
local R_max = 1.5*`R_tilde'
psacalc delta _IposXpartyb1, mcontrol(party2 _Imon* _Iyea* _Ipost2008_1 _Ipost2012_1 _Ipost2016_1 _IposXparty_1 _IposXpartya1) rmax(`R_max')
global delta = round(r(delta),0.01)



*Michigan data (columns 4 and 5)
use "$outputdata\IMmichiganfinal", clear
keep if yearpseudo>=2006
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2006/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)

*col 4: mich
# delimit ;
xi: regress ec_ice
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local fe "None"
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto est4

*col 5: mich, with countyxmonth fe
# delimit ;
xi: areg ec_ice
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(cmfe) robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
estadd local fe "County\(\times\)month", replace
est sto est5

label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1  "$\times$ Post 2008 election"
label var _IposXpartya1  "$\times$ Post 2012 election"
label var _IposXpartyb1 "$\times$ Post 2016 election"

#delimit ;
esttab est1 est2 est3 est4 est5 using "$tablesfigures\table_shock.tex",
width(\linewidth) alignment(c) 
keep(_Ipost* _IposX*) 
order(_Ipost2008_1 _Ipost2012_1 _Ipost2016_1 
_IposXparty_1 _IposXpartya1 _IposXpartyb1) 
replace label booktabs b(3) se(3) nomtitle noomitted
refcat(_IposXparty_1 "Republican affiliation", nolabel)
stats( N r2 fe, fmt(0 3) labels("Observations""\(R^{2}\)""FE"))
mgroups("Gallup: Economy getting better" "Michigan: Index of consumer expectations",
pattern(1 0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
nonotes addnotes("Oster (2019) test statistic for coefficient on Republican $\times$ Post 2016 election comparing columns 1 and 3 (null \$\beta^*=0\$, \$R_{max} = 1.5 R\$): \$\delta\$ = $delta." 
"* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01. Heteroskedasticity-robust standard errors clustered at the county level are in parantheses.")
substitute(\_ _);
#delimit cr   



********************************
*APPENDIX TABLE: Exploit panel dimension for Michigan Survey (TABLE A3)
*table_panel
********************************
eststo clear 
*Michigan data
use "$outputdata\IMmichiganfinal", clear
keep if yearpseudo>=2006
sort newid
by newid: keep if _N>=2
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2006/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)

*col 1
# delimit ;
xi: regress ec_ice
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local fe "N"
est sto est1

* col 2
# delimit ;
xi: areg ec_ice
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(newid) robust cluster(fips)
;
# delimit cr
estadd local fe "Y"
est sto est2


label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1  "$\times$ Post 2008 election"
label var _IposXpartya1  "$\times$ Post 2012 election"
label var _IposXpartyb1 "$\times$ Post 2016 election"

#delimit ;
esttab est1 est2 using "$tablesfigures\table_panel.tex", 
keep(_Ipost* _IposX*) 
order(_Ipost2008_1 _Ipost2012_1 _Ipost2016_1 
_IposXparty_1 _IposXpartya1 _IposXpartyb1) 
replace label f booktabs b(3) se(3) nomtitle noomitted
refcat(_IposXparty_1 "Republican affiliation", nolabel)
stats( N r2 fe, fmt(0 3) labels("Observations""\(R^{2}\)""Individual FE"))
mgroups("Michigan: Index of consumer expectations",
pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
;
#delimit cr   



********************************
*APPENDIX TABLE: OTHER MEASURES FROM SURVEY DATA (TABLE A2)
*table_other
********************************
est clear
*gallup data
use "$outputdata\IMgallupfinal", clear
keep if yearpseudo>=2008 & yearpseudo<=2016
keep if party2~=.
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2008/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)
egen zmfe=group(zipcode month)

*column 1
# delimit ;
xi: regress ec_cc
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto ec_cc

*Michigan data
use "$outputdata\IMmichiganfinal", clear
keep if yearpseudo>=2006
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2006/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}

*columns 2 through 7
foreach var of varlist ec_e12m ec_e5y ec_icc ec_gov ec_pexp ec_inexq1{
# delimit ;
xi: regress `var'
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
est sto `var'
estadd local repm "\specialcell{Vote \\ propensity}", replace
}

label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1  "$\times$ Post 2008 election"
label var _IposXpartya1  "$\times$ Post 2012 election"
label var _IposXpartyb1  "$\times$ Post 2016 election"

#delimit ;
esttab ec_cc ec_pexp ec_inexq1 ec_e12m ec_e5y ec_icc ec_gov using "$tablesfigures\table_other.tex", 
keep(_Ipost* _IposX*)  
order (_Ipost2008_1 _Ipost2012_1 _Ipost2016_1 _IposXparty_1 _IposXpartya1 _IposXpartyb1)
replace label f booktabs b(3) se(3) nonumber
mtitles("\specialcell{Rating of current\\economic conditions}"
"\specialcell{My financial\\situation, 1 year}"
"\specialcell{My expected\\income, 1 year}"
"\specialcell{Country business\\conditions, 12 months}"
"\specialcell{Country business\\conditions, 5 years}"
"\specialcell{Index of current\\economic conditions}"
"\specialcell{Government\\economic policy}")
refcat(_IposXrep_1 "Republican measure", nolabel)
stats( N r2, fmt(0 3) labels("Observations""\(R^{2}\)"))
mgroups("Gallup Survey" "Michigan Survey",
pattern(1 1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
;
#delimit cr   



********************************
*TABLE: IS YOUR BUSINESS HIRING? (TABLE A4)
*table_yourbiz
********************************
***Output LaTex (PG, 12/17/20 edit)
eststo clear
use "$outputdata\IMgallupfinal", clear
keep if yearpseudo>=2008 
keep if party2~=.
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2008/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)
egen zmfe=group(zipcode month)

*col 1
# delimit ;
eststo: xi: regress ec_yourbiz
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local fe "None"

*col 2: with countyxmonth
# delimit ;
eststo: xi: areg ec_yourbiz
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(cmfe) cluster(fips) robust
;
# delimit cr
estadd local fe "County\(\times\)month", replace

*col 3: with zipxmonth
# delimit ;
eststo: xi: areg ec_yourbiz
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(zmfe) cluster(fips) robust
;
# delimit cr
estadd local fe "ZIP\(\times\)month", replace

label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1 "$\times$ Post 2008 election"
label var _IposXpartya1 "$\times$ Post 2012 election"
label var _IposXpartyb1 "$\times$ Post 2016 election"

#delimit ;
esttab est1 est2 est3 using "$tablesfigures\table_yourbiz.tex", 
width(\linewidth) alignment(c)
keep(_Ipost* _IposX*) nomtitle
order ( _Ipost2008_1 _Ipost2012_1 _Ipost2016_1 )
replace label booktabs b(3) se(3) 
refcat(_IposXparty_1 "Republican affiliation", nolabel)
stats( N r2 fe, fmt(0 3) labels("Observations""\(R^{2}\)""FE"))
mgroups("\specialcell{Gallup: Is the business you work for hiring?}",
pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
nonotes addnotes("Oster (2019) test statistic for coefficient on Republican $\times$ Post 2016 election comparing columns 1 and 3 (null \$\beta^*=0\$, \$R_{max} = 1.5 R\$): \$\delta\$ = $delta." 
"* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01. Heteroskedasticity-robust standard errors clustered at the county level are in parantheses.")
substitute(\_ _);
#delimit cr   



*************************************************************************
*APPENDIX FIGURE ON PARTY AFFILIATION (FIGURE A1)
*fig_party
*************************************************************************
use "$inputdata\Igalluppanel", clear
drop if partyr==.
gen rep=(partyr==1)
gen ind=(partyr==2)
gen dem=(partyr==3)
collapse (mean) rep ind dem, by(year)
label var rep "Rep+lean"
label var ind "Independent"
label var dem "Dem+lean"
# delimit ;
graph twoway line rep ind dem year,
lc(maroon dkorange navy) lp(solid dash longdash) lw(thick thick thick)
ylabel(0 0.1 0.2 0.3 0.4 0.5 0.6) xtitle("") ytitle("Fraction") legend(rows(1));
# delimit cr
graph export "$tablesfigures\fig_party.eps", replace

**********************************************************
*APPENDIX FIGURES ON TRANSFERS, TAXES, AND PERSONAL INCOME (FIGURE A4)
**********************************************************

*COUNTY LEVEL TRANSFERS AND PERSONAL INCOME
use "$outputdata\CYsentiment", clear
foreach var of varlist pipc xtotpc xsocpc xmedpc xincpc xemppc xvetpc xedupc{
foreach num of numlist 1997 2001 2005 2009 2013{
gen t1=`var' if year==`num'
egen `var'`num'=min(t1), by(fips)
drop t1
gen `var'`num'gr=(`var'-`var'`num')/`var'`num'
}
}
sort fips year
*2000 election
foreach var of varlist pipc xtotpc xsocpc xmedpc xincpc xemppc xvetpc xedupc{
foreach num of numlist 1997/2008{
xi: regress `var'1997gr reppct2000  [aweight=pop2000] if year==`num', robust
gen cf`var'_`num'_1997=_coef[reppct2000]
gen hi`var'_`num'_1997=_coef[reppct2000]+2*_se[reppct2000]
gen lo`var'_`num'_1997=_coef[reppct2000]-2*_se[reppct2000]
}
foreach num of numlist 2005/2015{
xi: regress `var'2005gr reppct2008  [aweight=pop2008] if year==`num', robust
gen cf`var'_`num'_2005=_coef[reppct2008]
gen hi`var'_`num'_2005=_coef[reppct2008]+2*_se[reppct2008]
gen lo`var'_`num'_2005=_coef[reppct2008]-2*_se[reppct2008]
}
}
keep if _n==1
keep cf* hi* lo*
expand 20
gen year=_n+1996
foreach y in pipc xtotpc xsocpc xmedpc xincpc xemppc xvetpc xedupc{
gen cf`y'_1997=.
gen cf`y'_2005=.
gen hi`y'_1997=.
gen hi`y'_2005=.
gen lo`y'_1997=.
gen lo`y'_2005=.
foreach num of numlist 1997/2008{
replace cf`y'_1997=cf`y'_`num'_1997 if year==`num'
replace hi`y'_1997=hi`y'_`num'_1997 if year==`num'
replace lo`y'_1997=lo`y'_`num'_1997 if year==`num'
}
foreach num of numlist 2005/2015{
replace cf`y'_2005=cf`y'_`num'_2005 if year==`num'
replace hi`y'_2005=hi`y'_`num'_2005 if year==`num'
replace lo`y'_2005=lo`y'_`num'_2005 if year==`num'
}
}
drop *_20??_19?? *_19??_19?? *_20??_20??
sort year
save "$outputdata\cfgraphCY", replace

use "$outputdata\cfgraphCY", clear
# delimit ;
graph twoway scatter cfxtotpc_1997 hixtotpc_1997 loxtotpc_1997 year if year>=1997 & year<=2008,
connect(l l l) mc(maroon gray gray) m(Dh i i) lc(maroon gray gray) lw(thick thin thin) lp(solid dash dash)
legend(off) xtitle("") ytitle("Coefficient on Rep vote share", size(medsmall))
ylabel(, labs(medsmall))
xlabel(, labs(medsmall))
xline(2000, lc(black) lp(dash) lw(thin))
title("Growth in transfers, Bush", size(medium)) saving("$tablesfigures\fig_xfera.gph", replace);
# delimit cr
# delimit ;
graph twoway scatter cfxtotpc_2005 hixtotpc_2005 loxtotpc_2005 year if year>=2005 & year<=2015,
connect(l l l) mc(maroon gray gray) m(Dh i i) lc(maroon gray gray) lw(thick thin thin) lp(solid dash dash)
legend(off) xtitle("") ytitle("Coefficient on Rep vote share", size(medsmall))
ylabel(, labs(medsmall))
xlabel(, labs(medsmall))
xline(2008, lc(black) lp(dash) lw(thin))
title("Growth in transfers, Obama", size(medium)) saving("$tablesfigures\fig_xferb.gph", replace);
# delimit cr
# delimit ;
graph twoway scatter cfpipc_1997 hipipc_1997 lopipc_1997 year if year>=1997 & year<=2008,
connect(l l l) mc(maroon gray gray) m(Dh i i) lc(maroon gray gray) lw(thick thin thin) lp(solid dash dash)
legend(off) xtitle("") ytitle("Coefficient on Rep vote share", size(large))
ylabel(, labs(large))
xlabel(, labs(large))
xline(2000, lc(black) lp(dash) lw(thin))
title("Growth in personal income, Bush", size(huge)) saving("$tablesfigures\fig_pia.gph", replace);
# delimit cr
# delimit ;
graph twoway scatter cfpipc_2005 hipipc_2005 lopipc_2005 year if year>=2005 & year<=2015,
connect(l l l) mc(maroon gray gray) m(Dh i i) lc(maroon gray gray) lw(thick thin thin) lp(solid dash dash)
legend(off) xtitle("") ytitle("Coefficient on Rep vote share", size(large))
ylabel(, labs(large))
xlabel(, labs(large))
xline(2008, lc(black) lp(dash) lw(thin))
title("Growth in personal income, Obama", size(huge)) saving("$tablesfigures\fig_pib.gph", replace);
# delimit cr

*STATE LEVEL TAX RATES
use "$outputdata\SYsentiment", clear
foreach num of numlist 1997 2001 2005 2009 2013{
gen t1=taxrate if year==`num'
egen taxrate`num'=min(t1), by(state)
drop t1
gen taxrate`num'ch=(taxrate-taxrate`num')/taxrate`num'
}
sort state year
*2000 election
foreach num of numlist 1997/2008{
xi: regress taxrate1997ch reppct2000  [aweight=pop2000] if year==`num', robust
gen cftaxrate_`num'_1997=_coef[reppct2000]
gen hitaxrate_`num'_1997=_coef[reppct2000]+2*_se[reppct2000]
gen lotaxrate_`num'_1997=_coef[reppct2000]-2*_se[reppct2000]
}
*2008 election
foreach num of numlist 2005/2015{
xi: regress taxrate2005ch reppct2008  [aweight=pop2008] if year==`num', robust
gen cftaxrate_`num'_2005=_coef[reppct2008]
gen hitaxrate_`num'_2005=_coef[reppct2008]+2*_se[reppct2008]
gen lotaxrate_`num'_2005=_coef[reppct2008]-2*_se[reppct2008]
}
keep if _n==1
keep cf* hi* lo*
expand 20
gen year=_n+1996
gen cftaxrate_1997=.
gen cftaxrate_2005=.
gen hitaxrate_1997=.
gen hitaxrate_2005=.
gen lotaxrate_1997=.
gen lotaxrate_2005=.
foreach num of numlist 1997/2008{
replace cftaxrate_1997=cftaxrate_`num'_1997 if year==`num'
replace hitaxrate_1997=hitaxrate_`num'_1997 if year==`num'
replace lotaxrate_1997=lotaxrate_`num'_1997 if year==`num'
}
foreach num of numlist 2005/2015{
replace cftaxrate_2005=cftaxrate_`num'_2005 if year==`num'
replace hitaxrate_2005=hitaxrate_`num'_2005 if year==`num'
replace lotaxrate_2005=lotaxrate_`num'_2005 if year==`num'
}
drop *_20??_19?? *_19??_19?? *_20??_20??
sort year
save "$outputdata\cfgraphSY", replace

use "$outputdata\cfgraphSY", clear
# delimit ;
graph twoway scatter cftaxrate_1997 hitaxrate_1997 lotaxrate_1997 year if year>=1997 & year<=2008,
connect(l l l) mc(maroon gray gray) m(Dh i i) lc(maroon gray gray) lw(thick thin thin) lp(solid dash dash)
legend(off) xtitle("") ytitle("Coefficient on Rep vote share", size(medsmall)) yscale(titlegap(*-30))
ylabel(, labs(medsmall))
xlabel(, labs(medsmall))
xline(2000, lc(black) lp(dash) lw(thin))
title("Change in tax rate, Bush", size(medium)) saving("$tablesfigures\fig_taxratea.gph", replace);
# delimit cr
# delimit ;
graph twoway scatter cftaxrate_2005 hitaxrate_2005 lotaxrate_2005 year if year>=2005 & year<=2015,
connect(l l l) mc(maroon gray gray) m(Dh i i) lc(maroon gray gray) lw(thick thin thin) lp(solid dash dash)
legend(off) xtitle("") ytitle("Coefficient on Rep vote share", size(medsmall))
ylabel(, labs(medsmall))
xlabel(, labs(medsmall))
xline(2008, lc(black) lp(dash) lw(thin))
title("Change in tax rate, Obama", size(medium)) saving("$tablesfigures\fig_taxrateb.gph", replace);
# delimit cr

*fig_xfertaxes
graph combine "$tablesfigures\fig_xfera.gph" "$tablesfigures\fig_xferb.gph" "$tablesfigures\fig_taxratea.gph" "$tablesfigures\fig_taxrateb.gph", col(2) ysize(12) xsize(14)  saving("$tablesfigures\fig_xfertaxes.gph", replace)
graph export "$tablesfigures\fig_xfertaxes.eps", replace
*fig_pi
graph combine "$tablesfigures\fig_pia.gph" "$tablesfigures\fig_pib.gph", col(2) xsize(12) ysize(5)  saving("$tablesfigures\fig_pi.gph", replace)
graph export "$tablesfigures\fig_pi.eps", replace


********************************
*FIGURE FOR SURVEY MEASURES OF SPENDING (FIGURE 4 )
*fig_survey
********************************

*good time to buy car?
use "$outputdata\cfmichigangraph", clear
foreach var of varlist cfec_ice_2008-loec_inexq1_2016{
	replace `var'=. if `var'==0 & month~=10
}
# delimit ;
graph twoway scatter 
cfec_car_2008 
cfec_car_2016 eventtime,
connect(l l) mc(dkgreen maroon)
m(Th Dh) msize(large large)
lc(dkgreen  maroon)
lw(thick thick)
lp(longdash solid)
legend(order(1 2) rows(1) size(small)) xtitle("") ytitle("Coefficient on Republican", size(small))
ylabel(-3 -2 -1 0 1 2 3, labs(small))
xlabel(-4 -2 0 2 4 6, labs(small) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Michigan, car", size(medium)) saving("$tablesfigures\fig_car.gph", replace);
# delimit cr
*good time to buy durables?
# delimit ;
graph twoway scatter 
cfec_durables_2008 
cfec_durables_2016 eventtime,
connect(l l) mc(dkgreen maroon)
m(Th Dh) msize(large large)
lc(dkgreen  maroon)
lw(thick thick)
lp(longdash solid)
legend(order(1 2) rows(1) size(small)) xtitle("") ytitle("Coefficient on Republican", size(small))
ylabel(-3 -2 -1 0 1 2 3, labs(small))
xlabel(-4 -2 0 2 4 6, labs(small) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Michigan, household items", size(medium)) saving("$tablesfigures\fig_dur.gph", replace);
# delimit cr
 
**graphing coefficients
use "$outputdata\cfgallupgraph", clear
# delimit ;
graph twoway scatter 
cflspend_2008 cflspend_2009 cflspend_2010 cflspend_2011
cflspend_2012 cflspend_2013 cflspend_2014 cflspend_2015
cflspend_2016 eventtime,
connect(l l l l l l l l l) mc(dkgreen gray gray gray lavender gray gray gray maroon)
m(Th i i i Sh i i i Dh) msize(large vtiny vtiny vtiny large vtiny vtiny vtiny large)
lc(dkgreen gray gray gray lavender gray gray gray maroon)
lw(thick thin thin thin thick thin thin thin thick)
lp(longdash solid solid solid shortdash solid solid solid solid)
legend(order(1 5 9) rows(2) size(small)) xtitle("") ytitle("Coefficient on Republican", size(small))
ylabel(-0.4 -0.2 0 0.2 0.4, labs(small))
xlabel(-4 -2 0 2 4 6, labs(small) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Gallup, ln(spend yesterday)", size(medium)) saving("$tablesfigures\fig_spend.gph", replace);
# delimit cr

use "$outputdata\cfgallupspendgraph", clear
# delimit ;
graph twoway scatter 
cfec_spendcut_2009 cfec_spendcut_2010 cfec_spendcut_2011
cfec_spendcut_2012 cfec_spendcut_2013 cfec_spendcut_2014 cfec_spendcut_2015
cfec_spendcut_2016 eventtime,
connect(l l l l l l l l) mc(gray gray gray lavender gray gray gray maroon)
m(i i i Sh i i i Dh) msize(vtiny vtiny vtiny large vtiny vtiny vtiny large)
lc(gray gray gray lavender gray gray gray maroon)
lw(thin thin thin thick thin thin thin thick)
lp(solid solid solid shortdash solid solid solid solid)
legend(order(4 8) rows(2) size(small)) xtitle("") ytitle("Coefficient on Republican", size(small))
ylabel(-3 -2 -1 0 1 2 3, labs(small))
xlabel(-4 -2 0 2 4 6, labs(small) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Gallup, cutting back spending?", size(medium)) saving("$tablesfigures\fig_spendcut.gph", replace);
# delimit cr

graph combine "$tablesfigures\fig_car.gph" "$tablesfigures\fig_dur.gph" "$tablesfigures\fig_spend.gph" "$tablesfigures\fig_spendcut.gph", col(2) ysize(12) xsize(15)  saving("$tablesfigures\fig_survey.gph", replace)
graph export "$tablesfigures\fig_survey.eps", replace


********************************
*REGRESSION FOR SURVEY MEASURES OF SPENDING (TABLE 3)
*table_survey
********************************
use "$outputdata\IMgallupfinal", clear
keep if yearpseudo>=2008
keep if party2~=.
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2008/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)
egen zmfe=group(zipcode month)
gen lspend=ln(spend)

est clear 
**col 1: ln(spend yesterday), from gallup
# delimit ;
xi: regress lspend
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c1

**col 2: ln(spend yesterday) with zipxmonth fe, from gallup
# delimit ;
xi: areg lspend
i.month*party2 i.yearpseudo*party2
i.post2008*party2 i.post2012*party2 i.post2016*party2, absorb(zmfe) robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c2

**col 3: cutting back spending, from gallup
# delimit ;
xi, prefix(_pg_): regress ec_spendcut
i.month*party2 i.yearpseudo*party2
i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c3

**col 4: cutting back spending with zipxmonth fe, from gallup
# delimit ;
xi, prefix(_pg_): areg ec_spendcut
i.month*party2 i.yearpseudo*party2
i.post2012*party2 i.post2016*party2, absorb(zmfe) robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Partisan \\ affiliation}", replace
est sto c4


*Michigan
use "$outputdata\IMmichiganfinal", clear
keep if yearpseudo>=2006
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2006/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
egen cmfe=group(fips month)

**col 5: michigan, car question
# delimit ;
xi: regress ec_car
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Vote \\ propensity}", replace
est sto c5

**col 6: michigan, hh items question
# delimit ;
xi: regress ec_durables
i.month*party2 i.yearpseudo*party2 i.post2008*party2 i.post2012*party2 i.post2016*party2, robust cluster(fips)
;
# delimit cr
estadd local repm "\specialcell{Vote \\ propensity}", replace
est sto c6

label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1 "$\times$ Post 2008 election"
label var _IposXpartya1 "$\times$ Post 2012 election"
label var _IposXpartyb1 "$\times$ Post 2016 election"

#delimit ;
esttab c1 c2 c3 c4 c5 c6 using "$tablesfigures\table_survey.tex", 
rename(_Ipost2012_1 _Ipost2012 _pg_post201_1 _Ipost2012 
	   _Ipost2016_1 _Ipost2016 _pg_post201a1 _Ipost2016
	   _IposXpartya1 _IposXpty12 _pg_posXparty_1 _IposXpty12
	   _IposXpartyb1 _IposXpty16 _pg_posXpartya1 _IposXpty16) 
keep(_Ipost2008_1 _Ipost2012 _Ipost2016 _IposXparty_1 _IposXpty12 _IposXpty16)
order (_Ipost2008_1 _Ipost2012 _Ipost2016 _IposXparty_1 _IposXpty12 _IposXpty16)
replace f booktabs b(3) se(3) nonumber  nolabel
refcat(_IposXparty_1 "Republican affiliation", nolabel)
coeflabels(_Ipost2008_1 "Post 2008 election" _Ipost2012 "Post 2012 election" _Ipost2016 "Post 2016 election"
_IposXparty_1 "$\times$ Post 2008 election" _IposXpty12 "$\times$ Post 2012 election" 
_IposXpty16 "$\times$ Post 2016 election")
mtitles("\specialcell{Log spending\\yesterday}"
"\specialcell{Log spending\\yesterday, with\\ZIP\(\times\)month FE}"
"\specialcell{Not cutting\\back spending}"
"\specialcell{Not cutting\\back spending, with\\ZIP\(\times\)month FE}"
"\specialcell{Good time to\\buy a car}"
"\specialcell{Good time to\\buy major\\HH items}")
stats( N r2, fmt(0 3) labels("Observations""\(R^{2}\)"))
mgroups("Gallup Survey" "Michigan Survey",
pattern(1 0 0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
;
#delimit cr   


************************************
**AUTO SALES AND ARGUS LEVEL FIGURES (FIGURE 5)
**fig_level
***********************************
*AUTO SALES LEVELS
use "$outputdata\autovotegraph", clear
**2016, auto sales
# delimit ;
graph twoway scatter autosalesidx_d_p autosalesidx_r_p mydate if election==2016,
connect(l l) msymbol(Oh X) msize(large large) mc(navy maroon)
lw(thick thick) lc(navy maroon) lp(solid dash)
legend(size(small)) xtitle("") ytitle("Indexed to October", size(small))
ylabel(40 60 80 100 120 140 160 180, labs(small))
xlabel(, labs(small)) xline(682, lc(black) lp(shortdash) lw(thin))
title("2016 election, auto sales", size(medlarge)) saving("$tablesfigures\fig_auto16level", replace);
# delimit cr
**2008, auto sales
# delimit ;
graph twoway scatter autosalesidx_d_p autosalesidx_r_p mydate if election==2008,
connect(l l) msymbol(Oh X) msize(large large) mc(navy maroon)
lw(thick thick) lc(navy maroon) lp(solid dash)
legend(size(small)) xtitle("") ytitle("Indexed to October", size(small))
ylabel(40 60 80 100 120 140 160 180, labs(small))
xlabel(, labs(small))  xline(586, lc(black) lp(shortdash) lw(thin))
title("2008 election, auto sales", size(medlarge)) saving("$tablesfigures\fig_auto08level.gph", replace);
# delimit cr

*ARGUS LEVELS
**2008, argus
use "$outputdata\argusvotegraph", clear
# delimit ;
graph twoway scatter argusidx_d_p argusidx_r_p mydate if election==2008,
connect(l l) msymbol(Oh X) msize(large large) mc(navy maroon)
lw(thick thick) lc(navy maroon) lp(solid dash)
legend(size(small)) xtitle("") ytitle("Indexed to October", size(small))
ylabel(70 80 90 100 110 120 130, labs(small))
xlabel(, labs(small)) xline(586, lc(black) lp(shortdash) lw(thin))
title("2008 election, CC spending", size(medlarge)) saving("$tablesfigures\fig_argus08level", replace);
# delimit cr
**2016, argus
use "$outputdata\argusvotegraph", clear
# delimit ;
graph twoway scatter argusidx_d_p argusidx_r_p mydate if election==2016,
connect(l l) msymbol(Oh X) msize(large large) mc(navy maroon)
lw(thick thick) lc(navy maroon) lp(solid dash)
legend(size(small)) xtitle("") ytitle("Indexed to October", size(small))
ylabel(70 80 90 100 110 120 130, labs(small))
xlabel(, labs(small)) xline(682, lc(black) lp(shortdash) lw(thin))
title("2016 election, CC spending", size(medlarge)) saving("$tablesfigures\fig_argus16level", replace);
# delimit cr
graph combine "$tablesfigures\fig_auto16level" "$tablesfigures\fig_argus16level"  "$tablesfigures\fig_auto08level" "$tablesfigures\fig_argus08level", col(2) xsize(12) ysize(8) saving("$tablesfigures\fig_level", replace)
graph export "$tablesfigures\fig_level.eps", replace



***********************************************
**ADMINISTRATIVE SPENDING COEFFICIENT FIGURES (FIGURE 6)
***********************************************

*using vote measure
use "$outputdata\cfautovotegraph", clear
**auto sales
# delimit ;
graph twoway scatter 
cflautosales_2000 cflautosales_2001 cflautosales_2002 cflautosales_2003 cflautosales_2004
cflautosales_2005 cflautosales_2006 cflautosales_2007 cflautosales_2008 cflautosales_2009
cflautosales_2010 cflautosales_2011 cflautosales_2012 cflautosales_2013 cflautosales_2014
cflautosales_2015 cflautosales_2016 eventtime,
connect(l l l l l l l l l l l l l l l l l) mc(navy gray gray gray dkorange gray gray gray dkgreen gray gray gray lavender gray gray gray maroon)
m(X i i i Oh i i i Th i i i Sh i i i Dh) msize(vlarge vtiny vtiny vtiny large vtiny vtiny vtiny large vtiny vtiny vtiny large vtiny vtiny vtiny large)
lc(navy gray gray gray dkorange gray gray gray dkgreen gray gray gray lavender gray gray gray maroon)
lw(thick thin thin thin thick thin thin thin thick thin thin thin thick thin thin thin thick)
lp(solid solid solid solid dash solid solid solid longdash solid solid solid shortdash solid solid solid solid)
legend(order(1 5 9 13 17) rows(1)) xtitle("") ytitle("Coefficient on Rep vote propensity", size(medlarge))
ylabel(-1 -0.5 0 0.5 1 , labs(large))
xlabel(-4 -2 0 2 4 6, labs(large) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Auto sales", size(huge)) saving("$tablesfigures\fig_auto.gph", replace);
# delimit cr
**credit card
# delimit ;
graph twoway scatter 
cflargus_2006 cflargus_2007 cflargus_2008 cflargus_2009
cflargus_2010 cflargus_2011 cflargus_2012 cflargus_2014 cflargus_2015 cflargus_2016 eventtime,
connect(l l l l l l l l l l l) mc(gray gray dkgreen gray gray gray lavender gray gray maroon)
m(i i Th i i i Sh i i Dh) msize(vtiny vtiny large vtiny vtiny vtiny large vtiny)
lc(gray gray dkgreen gray gray gray lavender gray gray maroon)
lw(thin thin thick thin thin thin thick thin thin thick)
lp(solid solid longdash solid solid solid shortdash solid solid solid)
legend(order(3 7 10) rows(1)) xtitle("") ytitle("`y'", size(medium))
ylabel(-0.2 -0.1 0 0.1 0.2, labs(large)) ytitle("Coefficient on Rep vote propensity", size(medlarge))
xlabel(-4 -2 0 2 4 6, labs(large) valuelabels)
xline(0, lc(black) lp(dash) lw(thin))
title("Credit card spending", size(huge)) saving("$tablesfigures\fig_argus.gph", replace);
# delimit cr
**combined graph
graph combine "$tablesfigures\fig_auto.gph" "$tablesfigures\fig_argus.gph", col(2) xsize(12) ysize(5) saving("$tablesfigures\fig_county", replace)
graph export "$tablesfigures\fig_county.eps", replace



***************************************
**COUNTY LEVEL SPENDING REGRESSIONS (TABLE 4)
**table_county
********************************

use "$outputdata\CMsentiment", clear
keep if yearpseudo>=2000
capture drop post
gen lautosales=ln(autosales)
gen largus=ln(argus)
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2000/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2000 2004 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
est clear

**col 2: auto sales
# delimit ;
xi: regress lautosales
i.month*repvote i.yearpseudo*repvote
i.post2000*repvote i.post2004*repvote i.post2008*repvote i.post2012*repvote i.post2016*repvote [aweight=pop2008], robust
;
# delimit cr
est sto c2
label var _Ipost2000_1 "Post 2000 election" 
label var _Ipost2004_1 "Post 2004 election"
label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXrepvo_1  "$\times$ Post 2000 election"
label var _IposXrepvoa1  "$\times$ Post 2004 election"
label var _IposXrepvob1  "$\times$ Post 2008 election"
label var _IposXrepvoc1  "$\times$ Post 2012 election"
label var _IposXrepvod1  "$\times$ Post 2016 election"

**col 1: showing expectations work at county level
# delimit ;
regress ec_fc repvote  
_Imonth* _ImonX* 
_Iyearps*  _IyeaX* 
_Ipost2008_1 _IposXrepvob1 
_Ipost2012_1 _IposXrepvoc1
_Ipost2016_1 _IposXrepvod1 
[aweight=pop2008], robust
;
# delimit cr
est sto c1

**col 3: credit card lending
# delimit ;
regress largus  repvote  
_Imonth* _ImonX* 
_Iyearps*  _IyeaX* 
_Ipost2008_1 _IposXrepvob1 
_Ipost2012_1 _IposXrepvoc1
_Ipost2016_1 _IposXrepvod1
[aweight=pop2008], robust
;
# delimit cr
est sto c3

# delimit ;
esttab c1 c2 c3 using "$tablesfigures\table_county.tex", 
keep(_Ipost* _IposX*)  
order (_Ipost2000_1 _Ipost2004_1 _Ipost2008_1 _Ipost2012_1 _Ipost2016_1 _IposXrepvo_1 _IposXrepvoa1 _IposXrepvob1 _IposXrepvoc1 _IposXrepvod1)
replace f b(3) se(3) r2(3) nomtitles
refcat(_IposXrepvo_1 "Republican vote share", nolabel) 
mgroups("\specialcell{Economy getting\\better}" "\specialcell{Log auto\\sales}""\specialcell{Log credit card\\ spending}",
pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
booktabs label;
#delimit cr   



***************************************
**ZIP CODE LEVEL SPENDING REGRESSIONS (TABLE 5)
**table_zip
**************************************
**gallup regressions
use "$outputdata\ZMsentiment", clear
drop if party2gall==.
gen lautosales=ln(autosales)
gen largus=ln(argus)
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2000/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}
est clear

**col 1: 
# delimit ;
xi: regress ec_fc
i.month*party2gall i.yearpseudo*party2gall
i.post2008*party2gall i.post2012*party2gall i.post2016*party2gall [aweight=Ygallnum], robust cluster(fips)
;
# delimit cr
est sto c1

**col 2: 
# delimit ;
xi: regress lautosales
i.month*party2gall i.yearpseudo*party2gall
i.post2008*party2gall i.post2012*party2gall i.post2016*party2gall [aweight=Ygallnum], robust cluster(fips)
;
# delimit cr
est sto c2

**col 3:
# delimit ;
xi: regress largus
i.month*party2gall i.yearpseudo*party2gall
i.post2008*party2gall i.post2012*party2gall i.post2016*party2gall [aweight=Ygallnum], robust
;
# delimit cr
est sto c3

label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1 "$\times$ Post 2008 election"
label var _IposXpartya1 "$\times$ Post 2012 election"
label var _IposXpartyb1 "$\times$ Post 2016 election"

# delimit ;
esttab c1 c2 c3 using "$tablesfigures\table_zip.tex", 
keep(_Ipost* _IposX*)  
order (_Ipost2008_1 _Ipost2012_1 _Ipost2016_1 _IposXparty_1 _IposXpartya1 _IposXpartyb1)
replace f b(3) se(3) r2(3) noomitted nomtitles
refcat(_IposXparty_1 "Republican affiliation", nolabel)
mgroups("\specialcell{Economy getting\\better}" "\specialcell{Log auto\\ sales}""\specialcell{Log credit card\\ spending}",
pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
booktabs label;
#delimit cr   



***************************************
**ZIP CODE LEVEL TEST OF BORROWING CONSTRAINTS (TABLE A6)
**table_zip_bc
**************************************
**gallup regressions
use "$outputdata\ZMsentiment", clear
drop if party2gall==.
gen lautosales=ln(autosales)
gen largus=ln(argus)
sort zipcode year
merge m:1 zipcode year using "$inputdata/ZYirsclean.dta"
keep if _merge==1 | _merge==3
gen avinc=agi/returns
gen t=avinc if year==2010
egen avinc2010=min(t), by(zipcode)
xtile avinc2010q=avinc2010, nq(4)
keep if avinc2010q==4
gen post=0
replace post=1 if month<=4 | month>=11
foreach num of numlist 2000/2016{
gen month`num'=month*(yearpseudo==`num')
gen post`num'=post*(yearpseudo==`num')
}
char month[omit] 10 /*October*/
foreach num of numlist 2008 2012 2016{
char month`num'[omit] 10 /*October*/
}

**col 1: showing expectations work at zip code level
# delimit ;
xi: regress ec_fc
i.month*party2gall i.yearpseudo*party2gall
i.post2008*party2gall i.post2012*party2gall i.post2016*party2gall  [aweight=Ygallnum], robust cluster(fips)
;
# delimit cr
est sto c1

**col 2: car sales
# delimit ;
xi: regress lautosales
i.month*party2gall i.yearpseudo*party2gall
i.post2008*party2gall i.post2012*party2gall i.post2016*party2gall  [aweight=Ygallnum], robust cluster(fips)
;
# delimit cr
est sto c2

**col 3: credit card spending
# delimit ;
xi: regress largus
i.month*party2gall i.yearpseudo*party2gall
i.post2008*party2gall i.post2012*party2gall i.post2016*party2gall [aweight=Ygallnum], robust cluster(fips)
;
# delimit cr
est sto c3

label var _Ipost2008_1 "Post 2008 election"
label var _Ipost2012_1 "Post 2012 election"
label var _Ipost2016_1 "Post 2016 election"
label var _IposXparty_1 "$\times$ Post 2008 election"
label var _IposXpartya1 "$\times$ Post 2012 election"
label var _IposXpartyb1 "$\times$ Post 2016 election"

# delimit ;
esttab c1 c2 c3 using "$tablesfigures\table_zip_bc.tex", 
keep(_Ipost* _IposX*)  
order (_Ipost2008_1 _Ipost2012_1 _Ipost2016_1 _IposXparty_1 _IposXpartya1 _IposXpartyb1)
replace f b(3) se(3) r2(3) noomitted nomtitles
refcat(_IposXparty_1 "Republican affiliation", nolabel)
mgroups("\specialcell{Economy getting\\better}" "\specialcell{Log auto\\ sales}""\specialcell{Log credit card\\ spending}",
pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
booktabs label;
#delimit cr   



**************************************************************
*** BENCHMARK EXERCISE USING HOUSE PRICE GROWTH, 2006 to 2007 (FIGURE 7 AND TABLE 6)
***fig_scatter
***table_countyhp
**************************************************************
*2006 to 2007 house price decline
use "$outputdata\IMmichiganfinalFULL", clear
foreach var of varlist ec_ics ec_icc ec_ice ec_gov ec_durables ec_home ec_car ec_e5y ec_e12m ec_pexp{
sum `var'
replace `var'=(`var'-r(mean))/r(sd)
}
gen count=1
keep if year>=2004 & year<=2007
gen post=(year>=2007)
collapse (mean) ICE ec_ice ec_durables ec_home ec_car (sum) count, by(fips post)
sort fips post
by fips: keep if _N==2
egen countmin=min(count), by(fips)
keep if countmin>=5
sort fips post
foreach var of varlist ICE ec_ice ec_durables ec_home ec_car{
gen `var'ch=`var'-`var'[_n-1] if fips==fips[_n-1]
}
sort fips
merge m:1 fips using "$inputdata\Caddin"
keep if _merge==3
gen cl0607=ln(cl2007)-ln(cl2006)
# delimit ;
graph twoway 
(scatter ec_icech cl0607 [aweight=countmin], msize(medium) m(Oh))
(lfit ec_icech cl0607 [aweight=countmin]),
legend(off)
xtitle("House price growth, 2006 to 2007", size(large) margin(med)) xlabel(, labs(large))
ytitle("Change in ICE, 2007 minus 2004 through 2006", size(large)) ylabel(, labs(large))
title("Consumer expectations and House prices", size(huge))
saving("$tablesfigures\fig_scatterhpa", replace);
# delimit cr
**to add to auto sales to keep same counties
sort fips
by fips: keep if _n==1
keep fips countmin
save "$outputdata\Clisthp", replace
use "$outputdata\CMsentiment", clear
keep if year>=2006 & year<=2007
gen post=(year>=2007)
collapse (sum) autosales (mean) pop20?? cl20??, by(fips post)
sort fips
merge m:1 fips using "$outputdata\Clisthp"
keep if _merge==3
drop _merge
sort fips post
gen autosalesgr=(autosales-autosales[_n-1])/autosales[_n-1] if fips==fips[_n-1]
gen cl0607=ln(cl2007)-ln(cl2006)
# delimit ;
graph twoway 
(scatter autosalesgr cl0607 [aweight=countmin], msize(medium) m(Oh))
(lfit  autosalesgr cl0607 [aweight=countmin]),
legend(off)
xtitle("House price growth, 2006 to 2007", size(large) margin(med)) xlabel(, labs(large))
ytitle("Auto sales growth 2006 to 2007", size(large)) ylabel(-0.3 -0.2 -0.1 0 0.1 0.2 0.3, labs(large))
title("Auto sales and House prices", size(huge))
saving("$tablesfigures\fig_scatterhpb", replace);
# delimit cr
graph combine "$tablesfigures\fig_scatterhpa.gph" "$tablesfigures\fig_scatterhpb.gph", col(2) ysize(4) xsize(10)  saving("$tablesfigures\fig_scatter.gph", replace)
graph export "$tablesfigures\fig_scatter.eps", replace


*** table_countyhp
use "$outputdata\IMmichiganfinalFULL", clear
gen count=1
keep if year>=2004 & year<=2007
gen post=(year>=2007)
collapse (mean) ICE ec_ice ec_durables ec_home ec_car (sum) count, by(fips post)
sort fips post
by fips: keep if _N==2
egen countmin=min(count), by(fips)
sort fips post
foreach var of varlist ICE ec_ice ec_durables ec_home ec_car{
gen `var'ch=`var'-`var'[_n-1] if fips==fips[_n-1]
}
sort fips post
save "$outputdata\t1", replace
use "$outputdata\CMsentiment", clear
keep if year>=2006 & year<=2007
gen post=(year>=2007)
collapse (sum) autosales argus (mean) pop20?? cl20??, by(fips post)
sort fips post
merge 1:1 fips post using "$outputdata\t1"
keep if _merge==3
drop _merge
sort fips post
gen autosalesgr=(autosales-autosales[_n-1])/autosales[_n-1] if fips==fips[_n-1]
gen argusgr=(argus-argus[_n-1])/argus[_n-1] if fips==fips[_n-1]
gen cl0607=ln(cl2007)-ln(cl2006)
foreach var of varlist ec_icech ec_durablesch ec_homech ec_carch{
sum `var'
replace `var'=(`var'-r(mean))/r(sd)
}
*table_countyhp
estimates clear
regress ec_icech cl0607 [aweight=countmin], robust
est store m1
regress ec_durablesch cl0607 [aweight=countmin], robust
est store m2
regress ec_carch cl0607 [aweight=countmin], robust
est store m3
regress autosalesgr cl0607 [aweight=countmin], robust
est store m4
regress argusgr cl0607 [aweight=countmin], robust
est store m5

label var cl0607 "House price growth, 06 to 07"
label var ec_icech "$\Delta$ ICE, 04-06 to 07"

#delimit ;
esttab m1 m2 m3 m4 m5
using "$tablesfigures\table_countyhp.tex", 
replace f b(3) se(3) r2 nomtitles
mgroups("\specialcell{$\Delta$ ICE\\04-06 to 07} "
 "\specialcell{$\Delta$ Major HH items\\04-06 to 07}"
 "\specialcell{$\Delta$ Car\\04-06 to 07}"
 "\specialcell{Auto sales\\growth, 06 to 07 }"
 "\specialcell{Credit card spending\\growth, 06 to 07}",
pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))     
booktabs label;
#delimit cr            



********************************
*APPENDIX TABLE: CORRELATION IN MEASURES OF LOCAL/PERSONAL INCOME GROWTH (TABLE A7)
*table_indiv
********************************
*Michigan data
use "$outputdata\IMmichiganfinal", clear
foreach var of varlist ec_e12m ec_pexp ec_inexq1{
	egen `var'mydate=mean(`var'), by(mydate)
	gen `var'r=`var'-`var'mydate
}
pwcorr ec_e12mr ec_pexpr ec_inexq1r, sig
label var ec_e12mr "Country business conditions, 12 months"
label var ec_pexpr "My financial situation, 1 year"
label var ec_inexq1r "My expected income, 1 year"
eststo clear
estpost corr ec_e12mr ec_pexpr ec_inexq1r, matrix
eststo corrA
#delimit ;
esttab using "$tablesfigures\table_indivA.tex", 
replace f b(3) lines not noobs nonum 
starlevels(* 0.05 ** 0.01 *** 0.001)
booktabs label unstack;
#delimit cr


use "$outputdata\IMgallupfinal", clear
foreach var of varlist ec_fc ec_yourbiz{
	egen `var'mydate=mean(`var'), by(mydate)
	gen `var'r=`var'-`var'mydate
}
pwcorr ec_fcr ec_yourbizr, sig
label var ec_fcr "Economy getting better"
label var ec_yourbizr "Is the business you work for hiring?"
eststo clear
estpost corr ec_fcr ec_yourbizr, matrix
eststo corrB
#delimit ;
esttab using "$tablesfigures\table_indivB.tex", 
replace f b(3) lines not noobs nonum 
starlevels(* 0.05 ** 0.01 *** 0.001)
booktabs label unstack;
#delimit cr


